Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove unused SSR CSS files #10686

Closed
wants to merge 4 commits into from
Closed

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Sep 5, 2023

fixes #9161

This PR prevents copying the unused(?) CSS files that are generated from vite's build.ssrEmitAssets option. Kit seemingly only uses the client optimised CSS files in the .svelte-kit client bundle, so these server bundle css files shouldn't be copied over.

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

@changeset-bot
Copy link

changeset-bot bot commented Sep 5, 2023

🦋 Changeset detected

Latest commit: 104f19c

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

@benmccann
Copy link
Member

This doesn't seem safe to me. I'm not sure that we can know that the CSS is unused on the server. I think if it gets imported from a server file then this will break (#5240).

I had an earlier PR to fix the paths when you don't set paths.relative: true and I believe it got reverted. We could try to get that PR back in without whatever issue it was causing, which would solve this in most cases by avoiding the creation of duplicate CSS files (as long as you don't set paths.relative - maybe we could also fix that one by using relative paths on the server, but I'm not sure)

@eltigerchino
Copy link
Member Author

eltigerchino commented Sep 7, 2023

This doesn't seem safe to me. I'm not sure that we can know that the CSS is unused on the server. I think if it gets imported from a server file then this will break (#5240).

I don't think they're used anywhere because:

  1. import './styles.css' the server asset is never referenced in the built output.
  2. import style from './styles.css?inline' is always stored as a variable in the built js file.

I had an earlier PR to fix the paths when you don't set paths.relative: true and I believe it got reverted. We could try to get that PR back in without whatever issue it was causing, which would solve this in most cases by avoiding the creation of duplicate CSS files (as long as you don't set paths.relative - maybe we could also fix that one by using relative paths on the server, but I'm not sure)

The problem now also lies in the difference of filenames as well. I'm not sure what changed but the client css assets are now always named after the nodes such as 0.abs34af.css instead of _page.sd12sd.css. Building with the latest version of kit, there will always be duplicate CSS files even without the URL(...) path differences.

@benmccann
Copy link
Member

The case I was thinking of would be if you had a +server.js file with something like:

import css from './example.css';

export function GET() {
    return new Response(css + '\n\np { color: red }');
}

I don't know how likely that is, but it'd also be pretty weird that you can import anything you want into a +server.js file except for .css files which would magically break.

@eltigerchino
Copy link
Member Author

import css from './example.css';

I’ve tested this and the CSS gets inlined as well. Furthermore, this syntax is deprecated by Vite and is equivalent to import css from './example.css?inline'. Although, I’m not sure what that would mean once Vite officially stops supporting it.

@benmccann
Copy link
Member

It looks like Svelte 5 will stop outputting CSS in SSR mode. I wonder if we should just wait until then to avoid the extra code complication here

@eltigerchino
Copy link
Member Author

I agree. I think it's alright to wait until Svelte 5 releases.

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.

adapter-static SPA build output includes server / ssr css files
2 participants