diff --git a/.changeset/rare-coins-jump.md b/.changeset/rare-coins-jump.md new file mode 100644 index 000000000000..123539dd719c --- /dev/null +++ b/.changeset/rare-coins-jump.md @@ -0,0 +1,9 @@ +--- +"astro": minor +--- + +Supports adding the `data-astro-rerun` attribute on script tags so that they will be re-executed after view transitions + +```html + +``` diff --git a/packages/astro/src/transitions/router.ts b/packages/astro/src/transitions/router.ts index 817ada55c211..f32a6ae287e7 100644 --- a/packages/astro/src/transitions/router.ts +++ b/packages/astro/src/transitions/router.ts @@ -328,12 +328,15 @@ async function updateDOM( for (const s1 of document.scripts) { for (const s2 of beforeSwapEvent.newDocument.scripts) { if ( + // Check if the script should be rerun regardless of it being the same + !s2.hasAttribute('data-astro-rerun') && // Inline - (!s1.src && s1.textContent === s2.textContent) || - // External - (s1.src && s1.type === s2.type && s1.src === s2.src) + ((!s1.src && s1.textContent === s2.textContent) || + // External + (s1.src && s1.type === s2.type && s1.src === s2.src)) ) { - // the old script is in the new document: we mark it as executed to prevent re-execution + // the old script is in the new document and doesn't have the rerun attribute + // we mark it as executed to prevent re-execution s2.dataset.astroExec = ''; break; }