client.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { h, createSSRApp, createApp, Suspense } from 'vue';
  2. import { setup } from 'virtual:@astrojs/vue/app';
  3. import StaticHtml from './static-html.js';
  4. export default (element) =>
  5. async (Component, props, slotted, { client }) => {
  6. if (!element.hasAttribute('ssr')) return;
  7. // Expose name on host component for Vue devtools
  8. const name = Component.name ? `${Component.name} Host` : undefined;
  9. const slots = {};
  10. for (const [key, value] of Object.entries(slotted)) {
  11. slots[key] = () => h(StaticHtml, { value, name: key === 'default' ? undefined : key });
  12. }
  13. const isHydrate = client !== 'only';
  14. const bootstrap = isHydrate ? createSSRApp : createApp;
  15. const app = bootstrap({
  16. name,
  17. render() {
  18. let content = h(Component, props, slots);
  19. // related to https://github.com/withastro/astro/issues/6549
  20. // if the component is async, wrap it in a Suspense component
  21. if (isAsync(Component.setup)) {
  22. content = h(Suspense, null, content);
  23. }
  24. return content;
  25. },
  26. });
  27. await setup(app);
  28. app.mount(element, isHydrate);
  29. element.addEventListener('astro:unmount', () => app.unmount(), { once: true });
  30. };
  31. function isAsync(fn) {
  32. const constructor = fn?.constructor;
  33. return constructor && constructor.name === 'AsyncFunction';
  34. }