Skip to content

Conversation

@elliott-with-the-longest-name-on-github
Copy link
Contributor

Changes:

  • In load_css, grab the existing links' href attributes, which are absolute URLs to the CSS asset, and store them in a set
  • When checking to see if a link for an asset exists, first, resolve it as an absolute URL before checking that it exists in the set

I also switched the tests to use page.locactor, which will poll until the default timeout to see if something has changed. This needs to happen prior to checking the array. We really should use page.locator rather than page.textContent in general.

Observed behavior before this PR:

Basically, on server-side route resolution, load_css is re-loading CSS modules that are already in the document. You can see the behavior on pnpm -F test-basics build && pnpm -F test-basics preview by visiting http://localhost:4173/routing/form-get. Check the network requests for CSS, then type something into the input and click "submit". You'll see the CSS get loaded again.

Tracing, the problem here is:

The initial load includes these links in the head:

<link href="../_app/immutable/assets/0.DylTJ_5d.css" rel="stylesheet">
<link rel="stylesheet" crossorigin="" href="http://localhost:4173/_app/immutable/assets/1.CDE3T0tB.css">

On submission, the __route.js file returns this line:

import('../../_app/immutable/entry/start.D96TxtfR.js').then(x => x.load_css(['/_app/immutable/assets/1.CDE3T0tB.css','/_app/immutable/assets/0.DylTJ_5d.css',]));

load_css looks like this:

export function load_css(deps) {
	if (__SVELTEKIT_CLIENT_ROUTING__) return;

	const csp_nonce_meta = /** @type {HTMLMetaElement} */ (
		document.querySelector('meta[property=csp-nonce]')
	);
	const csp_nonce = csp_nonce_meta?.nonce || csp_nonce_meta?.getAttribute('nonce');

	for (const dep of deps) {
		if (dep in seen) continue;
		seen[dep] = true;
		if (document.querySelector(`link[href="${dep}"][rel="stylesheet"]`)) {
			continue;
		}

		const link = document.createElement('link');
		link.rel = 'stylesheet';
		link.crossOrigin = '';
		link.href = dep;
		if (csp_nonce) {
			link.setAttribute('nonce', csp_nonce);
		}
		document.head.appendChild(link);
	}
}

So it sees the following two deps:

/_app/immutable/assets/0.DylTJ_5d.css
/_app/immutable/assets/1.CDE3T0tB.css

However, the selector doesn't match, because the 0 href is relative.


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.

@changeset-bot
Copy link

changeset-bot bot commented Dec 2, 2025

🦋 Changeset detected

Latest commit: e87bf77

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

@Rich-Harris Rich-Harris merged commit ad71199 into main Dec 2, 2025
25 checks passed
@Rich-Harris Rich-Harris deleted the elliott/fix-broken-server-side-route-css-loading branch December 2, 2025 20:57
@github-actions github-actions bot mentioned this pull request Dec 2, 2025
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.

3 participants