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 6 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 links to be absolute to avoid console error when navigating
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
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 hrefs, because browsers will occasionally try to load relative
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
// URLs after a pushState/replaceState, resulting in a 404 — see
// https://github.com/sveltejs/kit/issues/3748
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
for (const link of document.querySelectorAll('link')) {
link.href = link.href; // eslint-disable-line
benmccann 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
20 changes: 16 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,27 @@ test.describe('$app/paths', () => {
);
});

test('replaces %sveltekit.assets% in template with relative path', async ({ page }) => {
test('replaces %sveltekit.assets% in template with relative path', async ({
benmccann marked this conversation as resolved.
Show resolved Hide resolved
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