Abort client-side navigation fetches when another link is clicked
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the bug
On any SvelteKit site with default link settings (all links have data-sveltekit-preload-data), hovering a link will prefetch that link's page's data and then clicking will navigate after all data has been fetched.
However, after clicking on a different link before the previous link has completed, the previous click's fetch will continue to run. This does not free up memory and network resources for the user as they wait for their desired page to load.
Reproduction
- Go to https://www.sveltelab.dev/ftydq4t92gq0r37
- Open dev tools network inspector and filter by
Fetch/XHR - Click on 'test' hyperlink, then immediately click on 'about' hyperlink
- Observe both requests are completed even though the 'test' page is no longer needed to load
Logs
No response
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 18.20.3 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.2.3 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
@sveltejs/adapter-auto: ^3.0.0 => 3.3.1
@sveltejs/kit: ^2.5.27 => 2.8.0
@sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.0
svelte: ^5 => 5.1.13
vite: ^5.4.4 => 5.4.10
Severity
annoyance
Additional Information
I think this can be addressed as a patch since cancelling unnecessary requests client-side will still complete their server-side load process to completion. If any applications depend on that behaviour, it shouldn't change.
Potentially out of scope is applying a similar 'abort' behaviour to goto and use:enhance for forms using <form method="GET">; though that's already solveable today in userland by providing wrappers.
Potentially related to
- https://github.com/sveltejs/kit/issues/9637; in which case perhaps we can add control to link behaviour via a client-side hook. Regardless, I consider the problem outlined above as a bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue at the linked SvelteLab example with the browser network inspector filtered to Fetch/XHR, then trace the client-side navigation and link-prefetch handling. Done means that clicking a second link aborts the earlier navigation fetch so only the requested page's fetch completes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100