Skip to content

fix: prevent duplicate CSS files after build - #9382

Merged
benmccann merged 5 commits into
masterfrom
s3812497-patch-1
Apr 9, 2023
Merged

benmccann merged 5 commits into
masterfrom
s3812497-patch-1

Conversation

@teemingc

@teemingc teemingc commented Mar 9, 2023 •

Copy link
Copy Markdown
Member

Prevents duplicate CSS files due to a different hash when the server CSS assets are not minified but the same client CSS is minified.
Explicitly specifying the Vite build option minify: 'esbuild' ensures they are always minified.
The default is esbuild but only minifies JS when it comes to server assets.

This is a workaround until Vite has been updated with vitejs/vite#12207 to separately specify CSS minification. This is also nice for those who haven't updated Vite.

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

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:.

@benmccann

Copy link
Copy Markdown
Member

vitejs/vite#12207 will be released any day now, so I guess the question is just which solution we'd prefer

@teemingc

Copy link
Copy Markdown
Member Author

I wasn't sure when the Vite update would be released. Feel free to close this PR if needed.

@dummdidumm

Copy link
Copy Markdown
Member

With that Vite PR being released now, what's the status here? Set this if we detect Vite<4.2? Anything that needs to be done for user that have 4.2 installed, or do they get this fix automatically?

@teemingc

Copy link
Copy Markdown
Member Author

Anything that needs to be done for user that have 4.2 installed, or do they get this fix automatically?

I did a quick test with sveltekit.new and it seems that vite 4.2 doesn't automatically fix the issue. Specifying either build.minifyCss: true or build.minify: 'esbuild' in user config fixes it. Haven't tried adding the new option directly to our build settings.

I think it's still best to go ahead with this fix since it's backwards compatible

Comment thread packages/kit/src/exports/vite/index.js Resolved
@changeset-bot

changeset-bot Bot commented Apr 8, 2023 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3752841

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

@bluwy bluwy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after a changeset

@teemingc

teemingc commented Apr 9, 2023

Copy link
Copy Markdown
Member Author

Thank you!

@frederikhors

Copy link
Copy Markdown
Contributor

I just updated to "@sveltejs/kit": "1.15.3" and the duplicated css for _layout.4434db71.css is gone. But is still there for many components. Is it ok (for now)?

@frederikhors

frederikhors commented Apr 11, 2023 •

Copy link
Copy Markdown
Contributor

I mean there are may duplicates, like:

  1. Select.7e949b76.css

  2. Select.b4485b05.css

  3. Custom.7e949b76.css

  4. Custom.b4485b05.css

  5. Attachment.b4485b05.css

  6. Attachment.b4485b05.css

and so on.

The hash is fake in this message.

@teemingc

Copy link
Copy Markdown
Member Author

I just updated to "@sveltejs/kit": "1.15.3" and the duplicated css for _layout.4434db71.css is gone. But is still there for many components. Is it ok (for now)?

Which type of adapter are you using? In some cases, the duplicate CSS files are correct, where URL paths will be different on the server vs the client.

The previous CSS duplicates were caused by differences such as values of 0.2 not being minified to .2, thus causing unnecessary duplicates. You can try to check if this is still the case.

@frederikhors

Copy link
Copy Markdown
Contributor

I'm using static adapter. But one of the two files is not used anywhere. If I search it by name in all the build dir I found nothing, the other is there. So one of those two files is generated but not used.

@teemingc

Copy link
Copy Markdown
Member Author

I'm using static adapter. But one of the two files is not used anywhere. If I search it by name in all the build dir I found nothing, the other is there. So one of those two files is generated but not used.

Got it. That's caused by the builder copying over everything (server + client). This currently can't be disabled at the adapter level, hence this issue. Same thing reported here #9161

@philsherry philsherry mentioned this pull request Nov 17, 2026
5 of 6 tasks
elliott-with-the-longest-name-on-github pushed a commit that referenced this pull request Nov 17, 2026
Related to #17794

`plugin_compile` sets `build.cssMinify` from `build.minify`, so a
`build.cssMinify` set in the user's Vite config is ignored. #9382 added
this as a workaround until Vite shipped a separate CSS minify option
(vitejs/vite#12207); that option is `build.cssMinify` and it now exists.

This uses `build.cssMinify` when the user sets it and keeps the existing
`minify`-derived value otherwise, so default behaviour is unchanged.
Server and client CSS still get matching hashes (the reason for #9382).
`build.cssMinify` isn't in `enforced_config`, so Kit doesn't currently
warn that it overrides it.

Without this, `build.cssMinify: 'esbuild'` is ignored and Vite 8 falls
back to Lightning CSS, which fails on CSS it can't parse (reproduction
in the linked issue).

This branch was successfully deployed

2 active deployments
Preview – kit-svelte-dev — 37528417 Deployed Apr 9, 2023 by vercel[bot]
Preview – kit — 37528417 Deployed Apr 9, 2023 by vercel[bot]
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.

5 participants