feat(spa): split persistent/static entrypoints
This commit is contained in:
parent
48db487bf1
commit
807e5c9ab2
5 changed files with 32 additions and 27 deletions
21
packages/integrations/spa/client-persist.js
Normal file
21
packages/integrations/spa/client-persist.js
Normal 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'));
|
||||
},
|
||||
});
|
8
packages/integrations/spa/client-static.js
Normal file
8
packages/integrations/spa/client-static.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
import listen from 'micromorph/spa';
|
||||
|
||||
export default () =>
|
||||
listen({
|
||||
afterDiff() {
|
||||
window.dispatchEvent(new CustomEvent('astro:locationchange'));
|
||||
},
|
||||
});
|
|
@ -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())
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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: {
|
||||
|
|
Loading…
Reference in a new issue