From 0a26a4de9c8614f0b752fa34c8cfc27495b01a27 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Sat, 26 Mar 2022 05:38:44 -0500 Subject: [PATCH] feat(spa): split persistent/static entrypoints --- packages/integrations/spa/client-persist.js | 21 +++++++++++++++++ packages/integrations/spa/client-static.js | 8 +++++++ packages/integrations/spa/client.js | 26 +-------------------- packages/integrations/spa/package.json | 2 +- packages/integrations/spa/src/index.ts | 2 +- 5 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 packages/integrations/spa/client-persist.js create mode 100644 packages/integrations/spa/client-static.js diff --git a/packages/integrations/spa/client-persist.js b/packages/integrations/spa/client-persist.js new file mode 100644 index 000000000000..89979db30359 --- /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 000000000000..9b003bf3d97d --- /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 23be7e78d77f..5f3ff77f2356 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 084ed3a15a39..ddf2aaff7689 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 0a848d100e8a..5d088fac3391 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: {