diff options
author | 2024-09-17 16:11:32 +0200 | |
---|---|---|
committer | 2024-09-17 16:11:32 +0200 | |
commit | a582cb61241b9c2a6f95900920145c055d5d6c12 (patch) | |
tree | bd2d734918ce74aca06ca81df0a0ade03fdf32b2 /packages/integrations/svelte/client.js | |
parent | daca9951f46b692f90e4760a9e285ef8accc7aaf (diff) | |
download | astro-a582cb61241b9c2a6f95900920145c055d5d6c12.tar.gz astro-a582cb61241b9c2a6f95900920145c055d5d6c12.tar.zst astro-a582cb61241b9c2a6f95900920145c055d5d6c12.zip |
Fix Svelte view transition state persistence (#12006)
Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com>
Diffstat (limited to 'packages/integrations/svelte/client.js')
-rw-r--r-- | packages/integrations/svelte/client.js | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/packages/integrations/svelte/client.js b/packages/integrations/svelte/client.js index 005bbe9da..288c7a661 100644 --- a/packages/integrations/svelte/client.js +++ b/packages/integrations/svelte/client.js @@ -3,6 +3,8 @@ const noop = () => {}; let originalConsoleWarning; let consoleFilterRefs = 0; +const existingApplications = new WeakMap(); + export default (element) => { return (Component, props, slotted, { client }) => { if (!element.hasAttribute('ssr')) return; @@ -14,18 +16,23 @@ export default (element) => { try { if (import.meta.env.DEV) useConsoleFilter(); - const component = new Component({ - target: element, - props: { - ...props, - $$slots: slots, - $$scope: { ctx: [] }, - }, - hydrate: client !== 'only', - $$inline: true, - }); + if (existingApplications.has(element)) { + existingApplications.get(element).$set({ ...props, $$slots: slots, $$scope: { ctx: [] } }); + } else { + const component = new Component({ + target: element, + props: { + ...props, + $$slots: slots, + $$scope: { ctx: [] }, + }, + hydrate: client !== 'only', + $$inline: true, + }); + existingApplications.set(element, component); - element.addEventListener('astro:unmount', () => component.$destroy(), { once: true }); + element.addEventListener('astro:unmount', () => component.$destroy(), { once: true }); + } } finally { if (import.meta.env.DEV) finishUsingConsoleFilter(); } |