Skip to content

Commit

Permalink
feat(spa): split persistent/static entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Moore committed Jun 1, 2022
1 parent a99acf0 commit 0a26a4d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
21 changes: 21 additions & 0 deletions packages/integrations/spa/client-persist.js
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions packages/integrations/spa/client-static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import listen from 'micromorph/spa';

export default () =>
listen({
afterDiff() {
window.dispatchEvent(new CustomEvent('astro:locationchange'));
},
});
26 changes: 1 addition & 25 deletions packages/integrations/spa/client.js
Original file line number Diff line number Diff line change
@@ -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())
2 changes: 1 addition & 1 deletion packages/integrations/spa/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/spa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 0a26a4d

Please sign in to comment.