Fix missing CSS for conditionally rendered Svelte components#16823
Conversation
…r conditionally rendered Svelte components
🦋 Changeset detectedLatest commit: ead3112 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
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [astro](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/astro)) | [`6.3.7` → `6.3.8`](https://renovatebot.com/diffs/npm/astro/6.3.7/6.3.8) |  |  | --- ### Release Notes <details> <summary>withastro/astro (astro)</summary> ### [`v6.3.8`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#638) [Compare Source](https://github.com/withastro/astro/compare/astro@6.3.7...astro@6.3.8) ##### Patch Changes - [#​16830](withastro/astro#16830) [`f2bf3cb`](withastro/astro@f2bf3cb) Thanks [@​matthewp](https://github.com/matthewp)! - Fixes 404s for dynamically imported JS chunks when using an adapter with `assetQueryParams` (e.g. Vercel skew protection) - [#​16831](withastro/astro#16831) [`ace96ba`](withastro/astro@ace96ba) Thanks [@​astrobot-houston](https://github.com/astrobot-houston)! - Fixes a misleading `GetStaticPathsRequired` error when a redirect is configured from a dynamic route to a static (or less-dynamic) destination. For example, `'/project/[slug]': '/'` previously produced a confusing error pointing at `index.astro`. Astro now detects the parameter mismatch at config validation time and throws a clear `InvalidRedirectDestination` error naming the missing parameters. - [#​16702](withastro/astro#16702) [`b7d1758`](withastro/astro@b7d1758) Thanks [@​matthewp](https://github.com/matthewp)! - Fixes scoped styles from `.astro` components being dropped when rendered inside MDX content (`<Content />` from `render(entry)`) passed through a named slot using `<Fragment slot="X">`. The Fragment component now eagerly evaluates its slot contents to ensure propagating components register their styles before head content is flushed. - [#​16823](withastro/astro#16823) [`3df6a45`](withastro/astro@3df6a45) Thanks [@​astrobot-houston](https://github.com/astrobot-houston)! - Fixes missing CSS for conditionally rendered Svelte components in production builds - [#​16836](withastro/astro#16836) [`3d7adfa`](withastro/astro@3d7adfa) Thanks [@​LongYC](https://github.com/LongYC)! - Document compressHTML: "jsx" config is only available since Astro v6.2.0 - [#​16864](withastro/astro#16864) [`334ce13`](withastro/astro@334ce13) Thanks [@​cheets](https://github.com/cheets)! - Fixes a false-positive `Internal Warning: route cache overwritten` logged on every SSR request for dynamic routes </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xOTUuNiIsInVwZGF0ZWRJblZlciI6IjQzLjE5NS42IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
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