summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2024-10-11 09:37:06 -0400
committerGravatar GitHub <noreply@github.com> 2024-10-11 09:37:06 -0400
commita4ffbfaa5cb460c12bd486fd75e36147f51d3e5e (patch)
treefdd05720cd66707c16c6f308e91b28609da8f4a7
parent411af55153970b89094a2bfe10206bc760023673 (diff)
downloadastro-a4ffbfaa5cb460c12bd486fd75e36147f51d3e5e.tar.gz
astro-a4ffbfaa5cb460c12bd486fd75e36147f51d3e5e.tar.zst
astro-a4ffbfaa5cb460c12bd486fd75e36147f51d3e5e.zip
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
-rw-r--r--.changeset/nervous-peaches-sort.md7
-rw-r--r--packages/astro/e2e/fixtures/view-transitions/src/assets/astro-build.mp4bin0 -> 467512 bytes
-rw-r--r--packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro7
-rw-r--r--packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro3
-rw-r--r--packages/astro/src/transitions/router.ts4
5 files changed, 17 insertions, 4 deletions
diff --git a/.changeset/nervous-peaches-sort.md b/.changeset/nervous-peaches-sort.md
new file mode 100644
index 000000000..337de234c
--- /dev/null
+++ b/.changeset/nervous-peaches-sort.md
@@ -0,0 +1,7 @@
+---
+'astro': patch
+---
+
+Ensure we target scripts for execution in the router
+
+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.
diff --git a/packages/astro/e2e/fixtures/view-transitions/src/assets/astro-build.mp4 b/packages/astro/e2e/fixtures/view-transitions/src/assets/astro-build.mp4
new file mode 100644
index 000000000..38e55d8c0
--- /dev/null
+++ b/packages/astro/e2e/fixtures/view-transitions/src/assets/astro-build.mp4
Binary files differ
diff --git a/packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro b/packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro
index 0a3a22913..936e25ceb 100644
--- a/packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro
+++ b/packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro
@@ -1,3 +1,6 @@
-<video controls="" autoplay="" name="media" transition:persist transition:name="video" autoplay>
- <source src="https://ia804502.us.archive.org/33/items/GoldenGa1939_3/GoldenGa1939_3_512kb.mp4" type="video/mp4">
+---
+import vidUrl from '../assets/astro-build.mp4';
+---
+<video controls="" autoplay="" transition:persist transition:name="video" autoplay>
+ <source src={vidUrl} type="video/mp4">
</video>
diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro
index 4f11cbbc0..c5bc118ed 100644
--- a/packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro
+++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro
@@ -20,4 +20,7 @@ import Layout from '../components/Layout.astro';
</custom-a>
<div id="test">test content</div>
+
+ <!-- This ensures we're correctly grabbing just scripts for execution -->
+ <div name="scripts"></div>
</Layout>
diff --git a/packages/astro/src/transitions/router.ts b/packages/astro/src/transitions/router.ts
index ddce9ff21..2be12895e 100644
--- a/packages/astro/src/transitions/router.ts
+++ b/packages/astro/src/transitions/router.ts
@@ -134,7 +134,7 @@ export function getFallback(): Fallback {
function runScripts() {
let wait = Promise.resolve();
- for (const script of Array.from(document.scripts)) {
+ for (const script of document.getElementsByTagName('script')) {
if (script.dataset.astroExec === '') continue;
const type = script.getAttribute('type');
if (type && type !== 'module' && type !== 'text/javascript') continue;
@@ -643,7 +643,7 @@ if (inBrowser) {
);
}
}
- for (const script of document.scripts) {
+ for (const script of document.getElementsByTagName('script')) {
script.dataset.astroExec = '';
}
}