Hash styles by filename rather than contents

#16636

Issue

Open
R
Rich-Harris
Aug 17, 2025, 12:10 PM

Describe the problem

Svelte adds svelte-xyz123 hashes to CSS rules and the corresponding elements, so that styles don't affect other components. This hash is based on the contents of the <style> block.

This works but it means that the hashes change frequently between deployments. It might be better to default to hashing based on the filename instead. I think the reason we didn't originally do this was that not all project setups made the filename available to the compiler, but now that most people are using vite-plugin-svelte that's probably not an issue any more.

Describe the proposed solution

Update this...

https://github.com/sveltejs/svelte/blob/7b2d7746277dae6372ed7b836ad895bde536dc89/packages/svelte/src/compiler/validate-options.js#L73-L75

...to this:

cssHash: fun(({ css, filename, hash }) => {
  return `svelte-${hash(filename ?? css)}`;
}),

Importance

nice to have

👍 9

Info

Assignees None
Labels css
Milestone None