From 0204b7de37bf626e1b97175b605adbf91d885386 Mon Sep 17 00:00:00 2001 From: Oliver Speir <115520730+OliverSpeir@users.noreply.github.com> Date: Fri, 8 Mar 2024 03:55:41 -0700 Subject: [PATCH] Reload scripts (#9977) * do not add astroExec if data-astro-reload attr exists * add changeset * Update .changeset/rare-coins-jump.md Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com> * change to data-astro-rerun * Update .changeset/rare-coins-jump.md Co-authored-by: Bjorn Lu * add example to changeset --------- Co-authored-by: Florian Lefebvre Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com> Co-authored-by: Happydev <81974850+MoustaphaDev@users.noreply.github.com> Co-authored-by: Bjorn Lu --- .changeset/rare-coins-jump.md | 9 +++++++++ packages/astro/src/transitions/router.ts | 11 +++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 .changeset/rare-coins-jump.md 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; }