Fix missing CSS for conditionally rendered Svelte components#16823
Fix missing CSS for conditionally rendered Svelte components#16823astrobot-houston wants to merge 2 commits into
Conversation
…r conditionally rendered Svelte components
🦋 Changeset detectedLatest commit: 10f1ced The changes in this PR will be included in the next version bump. This PR includes changesets to release 413 packages
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 |
|
| 📦 Package | 🔒 Before | 🔓 After |
|---|---|---|
| @opentelemetry/core | trusted-with-provenance | provenance |
| @opentelemetry/instrumentation | trusted-with-provenance | provenance |
| @opentelemetry/resources | trusted-with-provenance | provenance |
| @opentelemetry/sdk-trace-node | trusted-with-provenance | provenance |
| @opentelemetry/api-logs | trusted-with-provenance | provenance |
| @opentelemetry/context-async-hooks | trusted-with-provenance | provenance |
| @opentelemetry/semantic-conventions | trusted-with-provenance | provenance |
| require-in-the-middle | trusted-with-provenance | none |
| @opentelemetry/sdk-trace-base | trusted-with-provenance | provenance |
The restore logic was too broad — it restored any deleted CSS asset found in pagesToCss, but the normal parent walk also adds deleted CSS IDs there for components whose styles are already on the page from SSR. Track which component exports were rendered during SSR via ssrRenderedExports. Only flag deleted CSS for restore when the cssScopeTo target was NOT rendered in SSR (i.e. the styles were actually tree-shaken and missing from the page).
|
Here's where cssScopeTo is defined: https://github.com/vitejs/vite/blob/c917f1ef9d9c6ef131af96d89089d8ec680b18f2/packages/vite/src/node/plugins/css.ts#L675-L676 |
ematipico
left a comment
There was a problem hiding this comment.
Changeset needs to be addressed
| 'astro': patch | ||
| --- | ||
|
|
||
| Fixes CSS for conditionally rendered Svelte components being missing from production builds. When Svelte components are rendered behind `{#if}` blocks where the condition is `false` during SSR, Vite's `cssScopeTo` feature would tree-shake their CSS in the server build. The client build's CSS deduplication logic then incorrectly deleted the client CSS asset (which had the full styles) before the recovery code could use it. CSS assets are now saved before deletion and restored if they're needed by the `cssScopeTo` recovery logic. |
There was a problem hiding this comment.
This changeset isn't correct. @matthewp can you fix it? It contains technicalities that uses don't care
Changes
falseduring SSRssrRenderedExportsonBuildInternals)cssScopeTotarget component was not rendered in SSR — meaning the styles were genuinely tree-shaken and missing from the page. CSS for components that were rendered in SSR (and already on the page) stays deleted to avoid duplicate stylesheets.$state(false)with$effect()) instead of random condition that was passing by coincidenceTesting
packages/integrations/svelte/test/fixtures/conditional-rendering/src/components/Parent.svelteto use$state(false)instead ofMath.random() > 0.5for deterministic reproductionconditional-rendering.test.tsnow consistently verifies that conditionally rendered component CSS is included in production builds0-css.test.ts"remove unused styles from client:load components" test continues to pass (CSS for normally renderedclient:loadcomponents is still correctly deduplicated)Docs
Closes #16251