Skip to content

fix: reuse cached fetch responses when a cross-origin url is not in canonical form - #16339

Merged
Rich-Harris merged 3 commits into
sveltejs:version-3from
Nic-Polumeyv:fix-fetch-cache-url-normalization
Aug 17, 2026
Merged

fix: reuse cached fetch responses when a cross-origin url is not in canonical form#16339
Rich-Harris merged 3 commits into
sveltejs:version-3from
Nic-Polumeyv:fix-fetch-cache-url-normalization

Conversation

@Nic-Polumeyv

@Nic-Polumeyv Nic-Polumeyv commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

closes #14781

During SSR, fetch responses are embedded as <script data-sveltekit-fetched data-url=...> tags, and during hydration the client rebuilds the url to find them instead of hitting the network. The two sides build it differently. The server serializes the normalized href from new URL(input, event.url), while the client keeps the raw string, because prerendered pages may be served from any origin. That reasoning holds for same-origin urls, which are serialized path-relative, but cross-origin urls are absolute on both sides, so a raw string like http://localhost:8080 without the trailing slash misses the cache and the request fires a second time from the browser.

The fix normalizes requested to resolved.href in the cross-origin case only. Normalizing in build_selector instead, as suggested in the issue, would break the same-origin case, new URL('/mock') throws without a base. requested also became a const derived from resolved since the mutation was no longer needed.

The new test fails on main, rendering count: 2 after the hydration refetch overwrites the SSR data.

Not fixed here: request bodies are hashed as strings on the server but dropped from the client hash when they are not strings, so POST-with-body fetches still miss the cache. Can follow up separately.

Caching under the normalized href also meant a non-GET fetch spelled without the trailing slash could no longer evict the entry, so the second commit derives the eviction key the same way.

Also not fixed: on prerendered pages the server classifies same-origin against prerender.origin while the client uses the live origin, so an absolute url pointing at the deploy origin still misses the cache.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 14, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 5c0a735:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/5c0a735746fad6936592387352305ae163f19f4a

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16339

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5c0a735

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Nic-Polumeyv
Nic-Polumeyv changed the base branch from main to version-3 July 15, 2026 01:07
@Nic-Polumeyv
Nic-Polumeyv force-pushed the fix-fetch-cache-url-normalization branch from cb5e710 to dc68d3f Compare July 15, 2026 01:07
@Rich-Harris
Rich-Harris merged commit d8efec6 into sveltejs:version-3 Aug 17, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

load() fetch calls API twice if URL lacks trailing slash

2 participants