feat(spa): split persistent/static entrypoints

This commit is contained in:
Nate Moore 2022-03-26 05:38:44 -05:00 committed by Nate Moore
parent 48db487bf1
commit 807e5c9ab2
5 changed files with 32 additions and 27 deletions

View file

@ -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'));
},
});

View file

@ -0,0 +1,8 @@
import listen from 'micromorph/spa';
export default () =>
listen({
afterDiff() {
window.dispatchEvent(new CustomEvent('astro:locationchange'));
},
});

View file

@ -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())

View file

@ -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",

View file

@ -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: {