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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/forty-houses-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Prevent re-executing scripts in client router
23 changes: 22 additions & 1 deletion packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,14 +647,35 @@ test.describe('View Transitions', () => {
test('Scripts are only executed once', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
const p = page.locator('#one');
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');

// go to page 2
await page.click('#click-two');
const article = page.locator('#twoarticle');
await expect(article, 'should have script content').toHaveText('works');

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

// Go to page 3
await page.click('#click-three');
const article3 = page.locator('#three');
await expect(article3, 'should have content').toHaveText('Page 3');

// Go to page 5
await page.click('#click-five');
const article5 = page.locator('#five');
await expect(article5, 'should have content').toHaveText('Page 5');

// Go back to page 1
await page.goBack();
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.

const meta = page.locator('[name="script-executions"]');
await expect(meta).toHaveAttribute('content', '0');
});
Expand Down
12 changes: 9 additions & 3 deletions packages/astro/src/transitions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,17 @@ export function getFallback(): Fallback {
return 'animate';
}

const scriptsAlreadyRan = new Set<string>();
const detectScriptExecuted = (script: HTMLScriptElement) => {
const key = script.src ? new URL(script.src, location.href).href : script.textContent!;
if (scriptsAlreadyRan.has(key)) return true;
scriptsAlreadyRan.add(key);
return false;
};
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

const newScript = document.createElement('script');
Expand All @@ -149,7 +156,6 @@ function runScripts() {
}
newScript.setAttribute(attr.name, attr.value);
}
newScript.dataset.astroExec = '';
script.replaceWith(newScript);
}
return wait;
Expand Down Expand Up @@ -644,7 +650,7 @@ if (inBrowser) {
}
}
for (const script of document.getElementsByTagName('script')) {
script.dataset.astroExec = '';
detectScriptExecuted(script);
}
}

Expand Down
Loading