Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent rerunning scripts already ran in router #12985

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

matthewp
Copy link
Contributor

Changes

  • In a long session you might navigate between several pages, some contain the script and some not, but nevertheless the script should run a total of 1 time.
  • This also helps with smaller bundled scripts in production, where some are inlined.
  • Keeps track of scripts that have run, either by the src or by the inner text (for inline scripts) and uses that as a key, rather than the data attribute we previously adde.

Testing

  • Updated the existing tests to navigate between more pages.

Docs

N/A, bug fix.

In a long session you might navigate between several pages, some contain
the script and some not, but nevertheless the script should run a total
of 1 time.

This also helps with smaller bundled scripts in production, where some
are inlined.
Copy link

changeset-bot bot commented Jan 14, 2025

🦋 Changeset detected

Latest commit: fe845fe

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jan 14, 2025
@matthewp matthewp requested a review from martrapp January 14, 2025 20:45
Copy link

codspeed-hq bot commented Jan 14, 2025

CodSpeed Performance Report

Merging #12985 will not alter performance

Comparing rem-router-page (fe845fe) with main (1a026af)

Summary

✅ 6 untouched benchmarks

Copy link
Member

@martrapp martrapp left a comment

Choose a reason for hiding this comment

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

Looks like what we discussed on discord ;-)
Left two comments.

await page.goBack();
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');

Copy link
Member

Choose a reason for hiding this comment

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

Navigation across pages 3 and 5 will do a full page load as those don't use the client router.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah oops, ok I'll create a new page that does use it then.

function runScripts() {
let wait = Promise.resolve();
for (const script of document.getElementsByTagName('script')) {
if (script.dataset.astroExec === '') continue;
if (detectScriptExecuted(script)) continue;
const type = script.getAttribute('type');
if (type && type !== 'module' && type !== 'text/javascript') continue;
Copy link
Member

@martrapp martrapp Jan 15, 2025

Choose a reason for hiding this comment

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

maybe move checking the type attribute two lines up as we do not need to remember e.g. speculation rules, import maps, or partytown scripts

@martrapp
Copy link
Member

With that change in place, we can also remove

/*
* Mark new scripts that should not execute
*/
export function deselectScripts(doc: Document) {
for (const s1 of document.scripts) {
for (const s2 of doc.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))
) {
// 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;
}
}
}
}

and

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