0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

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 <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>
This commit is contained in:
Oliver Speir 2024-03-08 03:55:41 -07:00 committed by GitHub
parent 65692fa7b5
commit 0204b7de37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 4 deletions

View file

@ -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>
```

View file

@ -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;
}