feat: Support import styles from './index.module.css'#625
Merged
Conversation
This reverts commit b475626.
Deploying redwood-sdk-docs with
|
| Latest commit: |
9f10599
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://84af112b.redwood-sdk-docs.pages.dev |
| Branch Preview URL: | https://support-importing-styles.redwood-sdk-docs.pages.dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change adds support for importing stylesheets directly into client components, allowing developers to use
import './styles.css'orimport styles from './styles.module.css'inside "use client" modules.Problem
The framework did not previously have a mechanism to handle CSS imports within "use client" components.
The core issue stems from the framework's architecture, which uses a dynamic, server-rendered
Document.tsxcomponent and React Server Components (RSC). This differs from a standard Vite application in two key ways:index.htmlfile to discover all CSS dependencies during a build.As a result, there was no way to discover stylesheets imported by client components during a request. This meant that in a production build, these styles would be missing from the final HTML, and the development-time behavior was not suitable for a server-rendered application.
Solution
This change introduces a two-phase process designed to discover client-side script dependencies during a request and inject their associated stylesheets.
Script Discovery: At runtime, all client-side JavaScript modules required for the page are collected into a list. This includes static entry points found in
Document.tsxand dynamic components rendered as part of the RSC stream.Stylesheet Injection: Once the list of scripts is available, their CSS dependencies are looked up and injected in an environment-specific way.
<link>tags.<style>tags. Each tag includes adata-vite-dev-idattribute, which makes it compatible with Vite's Hot Module Replacement (HMR) system while preventing a Flash of Unstyled Content.For more, check out the Architecture Doc.
Testing
This change adds smoke tests to validate the new stylesheet handling functionality. Like the other smoke tests, these checks are done for both dev and deployments.The test cases cover:- Direct CSS imports in "use client" components (e.g.,import styles from './styles.module.css').- CSS URL imports inDocument.tsx(e.g.,import href from './styles.css?url').- Hot Module Replacement (HMR) for both import types to check that style changes are applied in devedit: Skipped for now, as only works for headed browsers
Links
Fixes #617