diff options
author | 2024-09-18 16:42:44 +0100 | |
---|---|---|
committer | 2024-09-18 16:43:14 +0100 | |
commit | b0827022afa52a13d49c37c6c549212aecd32cc5 (patch) | |
tree | 41e34f22f8fa388ca78e62a2ee802367d7e3cf42 /packages/integrations/vue/client.js | |
parent | 837ee3a4aa6b33362bd680d4a7fc786ed8639444 (diff) | |
parent | 8d4eb95086ae79339764d02215f84f4f21b96edc (diff) | |
download | astro-b0827022afa52a13d49c37c6c549212aecd32cc5.tar.gz astro-b0827022afa52a13d49c37c6c549212aecd32cc5.tar.zst astro-b0827022afa52a13d49c37c6c549212aecd32cc5.zip |
Merge branch 'main' into next
Diffstat (limited to 'packages/integrations/vue/client.js')
-rw-r--r-- | packages/integrations/vue/client.js | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/packages/integrations/vue/client.js b/packages/integrations/vue/client.js index 807f843fc..58ee11d5c 100644 --- a/packages/integrations/vue/client.js +++ b/packages/integrations/vue/client.js @@ -2,6 +2,9 @@ import { setup } from 'virtual:@astrojs/vue/app'; import { Suspense, createApp, createSSRApp, h } from 'vue'; import StaticHtml from './static-html.js'; +// keep track of already initialized apps, so we don't hydrate again for view transitions +let appMap = new WeakMap(); + export default (element) => async (Component, props, slotted, { client }) => { if (!element.hasAttribute('ssr')) return; @@ -15,21 +18,36 @@ export default (element) => const isHydrate = client !== 'only'; 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; - }, - }); - await setup(app); - app.mount(element, isHydrate); + // keep a reference to the app, props and slots so we can update a running instance later + let appInstance = appMap.get(element); + + if (!appInstance) { + appInstance = { + props, + slots, + }; + const app = bootstrap({ + name, + render() { + let content = h(Component, appInstance.props, appInstance.slots); + appInstance.component = this; + // 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; + }, + }); + await setup(app); + app.mount(element, isHydrate); + appMap.set(element, appInstance); + } else { + appInstance.props = props; + appInstance.slots = slots; + appInstance.component.$forceUpdate(); + } element.addEventListener('astro:unmount', () => app.unmount(), { once: true }); }; |