0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

Force re-execution of Partytown's head snippet on view transitions (#9666)

* Remove the header script before a view transition takes place to force a reload on the next page

* Add changeset

* Save another char
This commit is contained in:
Martin Trapp 2024-01-11 19:26:49 +01:00 committed by GitHub
parent 8059a19ad3
commit cdf8ce0627
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/partytown": patch
---
Fixes an issue where Partytown scripts didn't execute after view transition

View file

@ -21,6 +21,7 @@ export default function createPlugin(options?: PartytownOptions): AstroIntegrati
let partytownSnippetHtml: string;
const partytownEntrypoint = resolve('@builder.io/partytown/package.json');
const partytownLibDirectory = path.resolve(partytownEntrypoint, '../lib');
const SELF_DESTRUCT_ON_VIEW_TRANSITION = `;((d,s)=>(s=d.currentScript,d.addEventListener('astro:before-swap',()=>s.remove(),{once:true})))(document);`
return {
name: '@astrojs/partytown',
hooks: {
@ -32,6 +33,7 @@ export default function createPlugin(options?: PartytownOptions): AstroIntegrati
debug: options?.config?.debug ?? command === 'dev',
};
partytownSnippetHtml = partytownSnippet(partytownConfig);
partytownSnippetHtml += SELF_DESTRUCT_ON_VIEW_TRANSITION;
injectScript('head-inline', partytownSnippetHtml);
},
'astro:server:setup': ({ server }) => {
@ -60,4 +62,6 @@ export default function createPlugin(options?: PartytownOptions): AstroIntegrati
},
},
};
}