From cdf8ce06271b8b5e474186a3cd6d7925c423a4a6 Mon Sep 17 00:00:00 2001 From: Martin Trapp <94928215+martrapp@users.noreply.github.com> Date: Thu, 11 Jan 2024 19:26:49 +0100 Subject: [PATCH] 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 --- .changeset/afraid-suits-beam.md | 5 +++++ packages/integrations/partytown/src/index.ts | 4 ++++ 2 files changed, 9 insertions(+) create mode 100644 .changeset/afraid-suits-beam.md diff --git a/.changeset/afraid-suits-beam.md b/.changeset/afraid-suits-beam.md new file mode 100644 index 000000000000..d93cde160563 --- /dev/null +++ b/.changeset/afraid-suits-beam.md @@ -0,0 +1,5 @@ +--- +"@astrojs/partytown": patch +--- + +Fixes an issue where Partytown scripts didn't execute after view transition diff --git a/packages/integrations/partytown/src/index.ts b/packages/integrations/partytown/src/index.ts index cfe1293c995d..77dcfd76c13c 100644 --- a/packages/integrations/partytown/src/index.ts +++ b/packages/integrations/partytown/src/index.ts @@ -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 }, }, }; + + }