Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@astrojs/partytown): prevent crashes when View Transitions are enabled #11083

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/friendly-eagles-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@astrojs/partytown": patch
---

Prevent Partytown from crashing when View Transitions are enabled

When View Transitions are turned on, Partytown executes on every transition.
It's not meant to be like that, and therefore it breaks the integration completely.
Starting from now, Partytown will be executed only once.
4 changes: 2 additions & 2 deletions packages/integrations/partytown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ 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: {
'astro:config:setup': ({ config: _config, command, injectScript }) => {
const lib = `${appendForwardSlash(_config.base)}~partytown/`;
const recreateIFrameScript = `;(e=>{e.addEventListener("astro:before-swap",e=>{let r=document.body.querySelector("iframe[src*='${lib}']");e.newDocument.body.append(r)})})(document);`;
const partytownConfig = {
lib,
...options?.config,
debug: options?.config?.debug ?? command === 'dev',
};
partytownSnippetHtml = partytownSnippet(partytownConfig);
partytownSnippetHtml += SELF_DESTRUCT_ON_VIEW_TRANSITION;
partytownSnippetHtml += recreateIFrameScript;
injectScript('head-inline', partytownSnippetHtml);
},
'astro:server:setup': ({ server }) => {
Expand Down
Loading