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 and will display in the current browsing context #13165

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/witty-vans-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

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 @@ -135,7 +135,7 @@ export function get_link_info(a, base) {

const external =
!url ||
!!target ||
target === '_blank' ||
is_external_url(url, base) ||
(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">_self</a>
<a href="/routing/a?target" target="_top">_self</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 @@ -696,6 +696,32 @@
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 Expand Up @@ -828,7 +854,7 @@
expect(await page.textContent('h3')).toBe('bar');
});

test('responds to <form target="_blank"> submission with new tab', async ({ page }) => {

Check warning on line 857 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, ubuntu-latest, firefox, build)

flaky test: responds to <form target="_blank"> submission with new tab

retries: 2
await page.goto('/routing/form-target-blank');

let tabs = page.context().pages();
Expand All @@ -842,7 +868,7 @@
expect(tabs.length > 1);
});

test('responds to <button formtarget="_blank" submission with new tab', async ({ page }) => {

Check warning on line 871 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, ubuntu-latest, firefox, build)

flaky test: responds to <button formtarget="_blank" submission with new tab

retries: 2
await page.goto('/routing/form-target-blank');

let tabs = page.context().pages();
Expand Down Expand Up @@ -952,7 +978,7 @@

test.describe('Load', () => {
if (process.env.DEV) {
test('using window.fetch does not cause false-positive warning', async ({ page, baseURL }) => {

Check warning on line 981 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, macOS-latest, webkit, dev)

flaky test: using window.fetch does not cause false-positive warning

retries: 2
/** @type {string[]} */
const warnings = [];
page.on('console', (msg) => {
Expand Down
Loading