2023-03-31 12:42:03 -05:00
|
|
|
import { h, createSSRApp, createApp, Suspense } from 'vue';
|
2022-10-13 14:17:42 -05:00
|
|
|
import { setup } from 'virtual:@astrojs/vue/app';
|
2022-03-18 17:35:45 -05:00
|
|
|
import StaticHtml from './static-html.js';
|
|
|
|
|
2022-05-31 11:47:13 -05:00
|
|
|
export default (element) =>
|
2022-10-13 14:15:57 -05:00
|
|
|
async (Component, props, slotted, { client }) => {
|
2022-05-31 11:47:13 -05:00
|
|
|
if (!element.hasAttribute('ssr')) return;
|
2022-05-31 11:29:36 -05:00
|
|
|
|
2022-05-31 11:47:13 -05:00
|
|
|
// Expose name on host component for Vue devtools
|
|
|
|
const name = Component.name ? `${Component.name} Host` : undefined;
|
|
|
|
const slots = {};
|
2022-06-23 10:10:54 -05:00
|
|
|
for (const [key, value] of Object.entries(slotted)) {
|
|
|
|
slots[key] = () => h(StaticHtml, { value, name: key === 'default' ? undefined : key });
|
2022-05-31 11:47:13 -05:00
|
|
|
}
|
2023-03-31 12:42:03 -05:00
|
|
|
|
2023-08-29 09:30:11 -05:00
|
|
|
const isHydrate = client !== 'only';
|
2023-10-18 08:23:19 -05:00
|
|
|
const bootstrap = isHydrate ? createSSRApp : createApp;
|
|
|
|
const app = bootstrap({
|
|
|
|
name,
|
|
|
|
render() {
|
|
|
|
let content = h(Component, props, slots);
|
|
|
|
// related to https://github.com/withastro/astro/issues/6549
|
|
|
|
// if the component is async, wrap it in a Suspense component
|
|
|
|
if (isAsync(Component.setup)) {
|
|
|
|
content = h(Suspense, null, content);
|
|
|
|
}
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
});
|
2023-08-29 09:30:11 -05:00
|
|
|
await setup(app);
|
|
|
|
app.mount(element, isHydrate);
|
|
|
|
|
|
|
|
element.addEventListener('astro:unmount', () => app.unmount(), { once: true });
|
2022-05-31 11:47:13 -05:00
|
|
|
};
|
2023-03-31 12:42:03 -05:00
|
|
|
|
2023-03-31 12:44:00 -05:00
|
|
|
function isAsync(fn) {
|
|
|
|
const constructor = fn?.constructor;
|
2023-03-31 12:42:03 -05:00
|
|
|
return constructor && constructor.name === 'AsyncFunction';
|
|
|
|
}
|