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: navigate without reloading for links with a target attribute that will display in the current browsing context #13165

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/witty-vans-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
---

fix: navigate without reloading for links with a `target` attribute and will display in the current browsing context
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function get_link_info(a, base, uses_hash_router) {

const external =
!url ||
!!target ||
(target || '_self') !== '_self' ||
is_external_url(url, base, uses_hash_router) ||
(a.getAttribute('rel') || '').split(/\s+/).includes('external');

Expand Down
4 changes: 4 additions & 0 deletions packages/kit/test/apps/basics/src/routes/routing/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
<a href="/routing/b" data-sveltekit-reload>b</a>

<div class="hydrate-test"></div>

<a href="/routing/a?target" target="_self">_self</a>
<a href="/routing/a?target" target="_parent">_parent</a>
<a href="/routing/a?target" target="_top">_top</a>
26 changes: 26 additions & 0 deletions packages/kit/test/apps/basics/test/cross-platform/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,32 @@ test.describe('Routing', () => {
expect(requests.filter((url) => !url.endsWith('/favicon.png'))).toEqual([]);
});

test('navigates to a new page without reloading when `target` is the current browsing context', async ({
app,
page,
clicknav
}) => {
const targets = ['_self', '_parent', '_top'];

for (const target of targets) {
await page.goto('/routing');

await app.preloadData('/routing/a?target').catch((e) => {
// from error handler tests; ignore
if (!e.message.includes('Crashing now')) throw e;
});

/** @type {string[]} */
const requests = [];
page.on('request', (r) => requests.push(r.url()));

await clicknav(`a[target="${target}"]`);
expect(await page.textContent('h1')).toBe('a');

expect(requests.filter((url) => !url.endsWith('/favicon.png'))).toEqual([]);
}
});

test('navigates programmatically', async ({ page, app }) => {
await page.goto('/routing/a');
await app.goto('/routing/b');
Expand Down
Loading