Skip to content

Commit

Permalink
Reload scripts (#9977)
Browse files Browse the repository at this point in the history
* 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 <bjornlu.dev@gmail.com>

* add example to changeset

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
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 <bjornlu.dev@gmail.com>
  • Loading branch information
5 people committed Mar 8, 2024
1 parent 65692fa commit 0204b7d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .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
<script is:inline data-astro-rerun>...</script>
```
11 changes: 7 additions & 4 deletions packages/astro/src/transitions/router.ts
Expand Up @@ -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;
}
Expand Down

0 comments on commit 0204b7d

Please sign in to comment.