diff --git a/packages/integrations/spa/client-persist.js b/packages/integrations/spa/client-persist.js new file mode 100644 index 000000000..89979db30 --- /dev/null +++ b/packages/integrations/spa/client-persist.js @@ -0,0 +1,21 @@ +import listen from 'micromorph/spa'; + +export default () => + listen({ + beforeDiff(doc) { + for (const island of doc.querySelectorAll('astro-root')) { + const uid = island.getAttribute('uid'); + const current = document.querySelector(`astro-root[uid="${uid}"]`); + if (current) { + current.dataset.persist = true; + island.replaceWith(current); + } + } + }, + afterDiff() { + for (const island of document.querySelectorAll('astro-root')) { + delete island.dataset.persist; + } + window.dispatchEvent(new CustomEvent('astro:locationchange')); + }, + }); diff --git a/packages/integrations/spa/client-static.js b/packages/integrations/spa/client-static.js new file mode 100644 index 000000000..9b003bf3d --- /dev/null +++ b/packages/integrations/spa/client-static.js @@ -0,0 +1,8 @@ +import listen from 'micromorph/spa'; + +export default () => + listen({ + afterDiff() { + window.dispatchEvent(new CustomEvent('astro:locationchange')); + }, + }); diff --git a/packages/integrations/spa/client.js b/packages/integrations/spa/client.js index 23be7e78d..5f3ff77f2 100644 --- a/packages/integrations/spa/client.js +++ b/packages/integrations/spa/client.js @@ -1,25 +1 @@ -import listen from 'micromorph/spa'; - -export default ({ persistent }) => { - listen({ - beforeDiff(doc) { - if (!persistent) return; - for (const island of doc.querySelectorAll('astro-root')) { - const uid = island.getAttribute('uid'); - const current = document.querySelector(`astro-root[uid="${uid}"]`); - if (current) { - current.dataset.persist = true; - island.replaceWith(current); - } - } - }, - afterDiff() { - if (persistent) { - for (const island of doc.querySelectorAll('astro-root')) { - delete island.dataset.persist - } - } - window.dispatchEvent(new CustomEvent('astro:locationchange')); - }, - }); -}; +export default ({ persistent }) => (persistent ? import('./client-persist.js') : import('./client-static.js')).then(res => res.default()) diff --git a/packages/integrations/spa/package.json b/packages/integrations/spa/package.json index 084ed3a15..ddf2aaff7 100644 --- a/packages/integrations/spa/package.json +++ b/packages/integrations/spa/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/spa", "description": "SPA Astro Integrations", - "version": "0.0.2", + "version": "0.0.1", "type": "module", "types": "./dist/index.d.ts", "author": "withastro", diff --git a/packages/integrations/spa/src/index.ts b/packages/integrations/spa/src/index.ts index 0a848d100..5d088fac3 100644 --- a/packages/integrations/spa/src/index.ts +++ b/packages/integrations/spa/src/index.ts @@ -4,7 +4,7 @@ export interface SpaOptions { persistent?: boolean; } -export default function createPlugin({ persistent = true }: SpaOptions): AstroIntegration { +export default function createPlugin({ persistent = true }: SpaOptions = {}): AstroIntegration { return { name: '@astrojs/spa', hooks: {