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

[fix] transform links to be absolute to avoid console error when navigating #5583

Merged
merged 15 commits into from
Jul 20, 2022
Merged
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/stupid-dolls-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

[fix] transform link[rel=icon] to be absolute to avoid console error when navigating
7 changes: 7 additions & 0 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,13 @@ export function create_client({ target, session, base, trailing_slash }) {
}
});

// fix link[rel=icon], because browsers will occasionally try to load relative
// URLs after a pushState/replaceState, resulting in a 404 — see
// https://github.com/sveltejs/kit/issues/3748#issuecomment-1125980897
for (const link of document.querySelectorAll('link')) {
if (link.rel === 'icon') link.href = link.href;
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
}

addEventListener('pageshow', (event) => {
// If the user navigates to another site and then uses the back button and
// bfcache hits, we need to set navigating to null, the site doesn't know
Expand Down
22 changes: 18 additions & 4 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,15 +1043,29 @@ test.describe('$app/paths', () => {
);
});

test('replaces %sveltekit.assets% in template with relative path', async ({ page }) => {
// some browsers will re-request assets after a `pushState`
// https://github.com/sveltejs/kit/issues/3748#issuecomment-1125980897
test('replaces %sveltekit.assets% in template with relative path, and makes it absolute in the client', async ({
baseURL,
page,
javaScriptEnabled
}) => {
const absolute = `${baseURL}/favicon.png`;

await page.goto('/');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('./favicon.png');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe(
javaScriptEnabled ? absolute : './favicon.png'
);

await page.goto('/routing');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('./favicon.png');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe(
javaScriptEnabled ? absolute : './favicon.png'
);

await page.goto('/routing/rest/foo/bar/baz');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe('../../../../favicon.png');
expect(await page.getAttribute('link[rel=icon]', 'href')).toBe(
javaScriptEnabled ? absolute : '../../../../favicon.png'
);
});
});

Expand Down