Skip to content

fix: lazily load CSS for CSR dynamically imported components#13564

Merged
dummdidumm merged 8 commits intomainfrom
fix-dynamic-import-css
Mar 14, 2025
Merged

fix: lazily load CSS for CSR dynamically imported components#13564
dummdidumm merged 8 commits intomainfrom
fix-dynamic-import-css

Conversation

@teemingc
Copy link
Member

@teemingc teemingc commented Mar 11, 2025

fixes #13546

This PR changes server nodes from always loading dynamically imported component's styles and fonts to only loading them if they are used during SSR (to avoid FOUC). This fixes lazy loading of components on the client, only retrieving the styles when needed.

We can tell which deps are used during SSR by analysing the deps of the node with both the server manifest and the client manifest, then comparing the results. If a style or font is missing from the server deps, it's probably only used on the client. Therefore, it should be safe to load lazily.


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 Mar 11, 2025

🦋 Changeset detected

Latest commit: f1e419d

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

@teemingc teemingc marked this pull request as draft March 11, 2025 07:21
@teemingc teemingc changed the title fix: lazily load CSS for dynamically imported components fix: lazily load CSS for CSR dynamically imported components Mar 12, 2025
@teemingc teemingc marked this pull request as ready for review March 12, 2025 11:23
@onesinghashish
Copy link

@eltigerchino thanks for the fix. eagerly waiting for the release :)

@teemingc
Copy link
Member Author

teemingc commented Mar 14, 2025

@eltigerchino thanks for the fix. eagerly waiting for the release :)

You can try it out right now while waiting for the official release. Just run:

# installs SvelteKit with changes from this PR
npm i -D https://pkg.pr.new/sveltejs/kit/@sveltejs/kit@27e37ab

Any feedback is welcome.

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

nice!

@dummdidumm dummdidumm merged commit 001bc04 into main Mar 14, 2025
16 checks passed
@dummdidumm dummdidumm deleted the fix-dynamic-import-css branch March 14, 2025 22:03
@github-actions github-actions bot mentioned this pull request Mar 14, 2025
@onesinghashish
Copy link

@eltigerchino thanks for the fix. eagerly waiting for the release :)

You can try it out right now while waiting for the official release. Just run:

# installs SvelteKit with changes from this PR
npm i -D https://pkg.pr.new/sveltejs/kit/@sveltejs/kit@27e37ab

Any feedback is welcome.

@eltigerchino works great. thanks

Copilot AI pushed a commit to Stadly/kit that referenced this pull request Mar 6, 2026
…s#13564)

fixes sveltejs#13546

This PR changes server nodes from always loading dynamically imported component's styles and fonts to only loading them if they are used during SSR (to avoid FOUC). This fixes lazy loading of components on the client, only retrieving the styles when needed.

We can tell which deps are used during SSR by analysing the deps of the node with both the server manifest and the client manifest, then comparing the results. If a style or font is missing from the server deps, it's probably only used on the client. Therefore, it should be safe to load lazily.
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.

SvelteKit loads CSS of dynamically imported components upfront when the page loads

3 participants