Skip to content

Commit a4ffbfa

Browse files
authored
Ensure router only targets scripts for execution (#12177)
* Ensure router only targets scripts for execution * Add a test * Move the test up to the file that's testing * remove extra prop * just see if tests pass * use local file * smaller file * use getElementsByTagName again
1 parent 411af55 commit a4ffbfa

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

.changeset/nervous-peaches-sort.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Ensure we target scripts for execution in the router
6+
7+
Using `document.scripts` is unsafe because if the application has a `name="scripts"` this will shadow the built-in `document.scripts`. Fix is to use `getElementsByTagName` to ensure we're only grabbing real scripts.
Binary file not shown.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
<video controls="" autoplay="" name="media" transition:persist transition:name="video" autoplay>
2-
<source src="https://ia804502.us.archive.org/33/items/GoldenGa1939_3/GoldenGa1939_3_512kb.mp4" type="video/mp4">
1+
---
2+
import vidUrl from '../assets/astro-build.mp4';
3+
---
4+
<video controls="" autoplay="" transition:persist transition:name="video" autoplay>
5+
<source src={vidUrl} type="video/mp4">
36
</video>

packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ import Layout from '../components/Layout.astro';
2020
</custom-a>
2121

2222
<div id="test">test content</div>
23+
24+
<!-- This ensures we're correctly grabbing just scripts for execution -->
25+
<div name="scripts"></div>
2326
</Layout>

packages/astro/src/transitions/router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function getFallback(): Fallback {
134134

135135
function runScripts() {
136136
let wait = Promise.resolve();
137-
for (const script of Array.from(document.scripts)) {
137+
for (const script of document.getElementsByTagName('script')) {
138138
if (script.dataset.astroExec === '') continue;
139139
const type = script.getAttribute('type');
140140
if (type && type !== 'module' && type !== 'text/javascript') continue;
@@ -643,7 +643,7 @@ if (inBrowser) {
643643
);
644644
}
645645
}
646-
for (const script of document.scripts) {
646+
for (const script of document.getElementsByTagName('script')) {
647647
script.dataset.astroExec = '';
648648
}
649649
}

0 commit comments

Comments
 (0)