Skip to content

fix(transitions): snapshot script collection before iterating in runScripts - #17563

Open
okxint wants to merge 1 commit into
withastro:mainfrom
okxint:fix/run-scripts-live-collection
Open

fix(transitions): snapshot script collection before iterating in runScripts#17563
okxint wants to merge 1 commit into
withastro:mainfrom
okxint:fix/run-scripts-live-collection

Conversation

@okxint

@okxint okxint commented Jul 31, 2026

Copy link
Copy Markdown

getElementsByTagName returns a live HTMLCollection. When new scripts are
appended to the DOM inside the loop body — specifically the sentinel
data:application/javascript module injected at line 142 — the collection
grows mid-iteration and subsequently added scripts are skipped (the iterator
index advances past them before they are visited).

Convert to a static snapshot with Array.from() before each loop so every
script is visited exactly once.

Affected loops (runScripts() is called on every page transition):

  • First pass at line 131 — detects whether a sentinel is needed
  • Second pass at line 142 — runs/re-runs scripts
  • Initialisation pass at line 681 — marks scripts already executed

…cripts

getElementsByTagName returns a live HTMLCollection. When new scripts are
appended to the DOM inside the loop body (e.g. the sentinel
data:application/javascript module), the collection grows mid-iteration and
some scripts get skipped. Convert to a static Array snapshot with
Array.from() before each loop so every script is visited exactly once.
@changeset-bot

changeset-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: c15e0a2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label Jul 31, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 31, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 21 untouched benchmarks


Comparing okxint:fix/run-scripts-live-collection (c15e0a2) with main (9f4c1da)

Open in CodSpeed

@sirrryasir sirrryasir left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting document.getElementsByTagName('script') to static arrays via Array.from() across runScripts() and the initialization pass is correct.

getElementsByTagName returns a live HTMLCollection. When runScripts() appends the sentinel module script (data:application/javascript,) at line 142, the live collection updates mid-loop, causing index displacement and skipping subsequent script tags. Static array snapshots ensure every element present at invocation is visited exactly once.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants