Skip to content

Commit

Permalink
fix: only preload link if the URL differs from current page (#12773)
Browse files Browse the repository at this point in the history
fixes #10234

By avoiding preloading pages where the URL pathname and search params (the load ID) are unchanged, we also avoid mistakenly caching navigation results for hash URLs. Currently, these are ID'd the same as URLs without hashes, causing navigation to lose track of the current URL (hence the original issue).

This should be a good solution because:

- if we're on already on the page we don't need new load results for it.
- even if the load results are suppose to be different for the same URL, clicking on a link that leads to the exact same URL already does nothing.
  • Loading branch information
eltigerchino authored Oct 9, 2024
1 parent 5f1de9f commit 571f731
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/calm-fireants-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: only preload links that have a different URL than the current page
5 changes: 4 additions & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,10 @@ function setup_preload() {

const options = get_router_options(a);

if (!options.reload) {
// we don't want to preload data for a page we're already on
const same_url = url && current.url.pathname + current.url.search === url.pathname + url.search;

if (!options.reload && !same_url) {
if (priority <= options.preload_data) {
const intent = get_navigation_intent(url, false);
if (intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
<a href="#hash-target">hash link</a>
<a href="/routing/hashes/b">b</a>
<a href="#replace-state" data-sveltekit-replacestate>replace state</a>

<a data-sveltekit-preload-data href="/routing/hashes/a">/routing/hashes/a</a>
<a data-sveltekit-preload-data href="#preload">#preload</a>
13 changes: 13 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 @@ -615,6 +615,19 @@ test.describe('Prefetching', () => {
await expect(page.locator('h1')).not.toHaveText('Oopsie');
});

test('same route hash links work more than once', async ({ page, clicknav, baseURL }) => {
await page.goto('/routing/hashes/a');

await clicknav('[href="#preload"]');
await expect(page.url()).toBe(`${baseURL}/routing/hashes/a#preload`);

await clicknav('[href="/routing/hashes/a"]');
await expect(page.url()).toBe(`${baseURL}/routing/hashes/a`);

await clicknav('[href="#preload"]');
await expect(page.url()).toBe(`${baseURL}/routing/hashes/a#preload`);
});

test('does not rerun load on calls to duplicate preload hash route', async ({ app, page }) => {
await page.goto('/routing/a');

Expand Down

0 comments on commit 571f731

Please sign in to comment.