Repository navigation
Stable CSS Module class names in dev to prevent HMR stale class name mismatch #18164
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
Merged
+240
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
83b3982
fix(dev): use stable CSS Module class names in dev to fix HMR for mix…
factory[bot] 594052b
fix: address review feedback on CSS Module dev HMR
factory[bot] 74025aa
fix: address review feedback on CSS Module dev HMR
factory[bot] 6555092
Merge branch 'main' into factory/fix-18105
matthewp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'astro': patch | ||
| --- | ||
|
|
||
| Fixes CSS Module HMR in dev when a component is rendered both with and without hydration on the same page. Astro now uses path-based class name hashing in dev mode so that editing CSS declarations no longer changes the generated selectors, allowing Vite's CSS HMR to update styles without a full page reload. |
17 changes: 17 additions & 0 deletions
17
packages/astro/e2e/fixtures/hmr/src/pages/scss-module-ssr-and-island.astro
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| import ScssModuleHeading from '../components/ScssModuleHeading.jsx'; | ||
| --- | ||
|
|
||
| <html> | ||
| <head> | ||
| <title>Test</title> | ||
| </head> | ||
| <body> | ||
| <div id="ssr"> | ||
| <ScssModuleHeading /> | ||
| </div> | ||
| <div id="island"> | ||
| <ScssModuleHeading client:load /> | ||
| </div> | ||
| </body> | ||
| </html> |
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
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
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
7 changes: 7 additions & 0 deletions
7
packages/astro/test/fixtures/0-css/src/components/CssModuleHmr.jsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import Styles from './CssModuleHmr.module.css'; | ||
|
|
||
| export default function CssModuleHmr() { | ||
| return ( | ||
| <h1 className={Styles.title}>CSS Module HMR</h1> | ||
| ); | ||
| } |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/0-css/src/components/CssModuleHmr.module.css
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .title { | ||
| font-family: fantasy; | ||
| } |
17 changes: 17 additions & 0 deletions
17
packages/astro/test/fixtures/0-css/src/pages/css-module-hmr.astro
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| import CssModuleHmr from '../components/CssModuleHmr.jsx'; | ||
| --- | ||
|
|
||
| <html> | ||
| <head> | ||
| <title>CSS Module HMR</title> | ||
| </head> | ||
| <body> | ||
| <div id="ssr"> | ||
| <CssModuleHmr /> | ||
| </div> | ||
| <div id="island"> | ||
| <CssModuleHmr client:load /> | ||
| </div> | ||
| </body> | ||
| </html> |
82 changes: 82 additions & 0 deletions
82
packages/astro/test/units/dev/css-module-scoped-name.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import * as assert from 'node:assert/strict'; | ||
|
astro-factory[bot] marked this conversation as resolved.
|
||
| import { describe, it } from 'node:test'; | ||
| import { createVite } from '../../../dist/core/create-vite.js'; | ||
| import { createBasicSettings, defaultLogger } from '../test-utils.ts'; | ||
|
|
||
| type GenerateScopedName = (name: string, filename: string, css: string) => string; | ||
|
|
||
| describe('CSS module scoped name in dev', () => { | ||
| it('provides a stable generateScopedName in dev mode', async () => { | ||
| const settings = await createBasicSettings(); | ||
| const routesList = { routes: [] }; | ||
| const config = await createVite( | ||
| {}, | ||
| { | ||
| settings, | ||
| logger: defaultLogger, | ||
| mode: 'development', | ||
| command: 'dev', | ||
| routesList, | ||
| sync: false, | ||
| }, | ||
| ); | ||
|
|
||
| const generateScopedName = config.css?.modules | ||
| ? (config.css.modules as { generateScopedName?: GenerateScopedName }).generateScopedName | ||
| : undefined; | ||
|
|
||
| assert.ok( | ||
| typeof generateScopedName === 'function', | ||
| 'dev config should have generateScopedName', | ||
| ); | ||
|
|
||
| // Same file + class name should produce the same result regardless of CSS content | ||
| const name1 = generateScopedName('card', '/src/Card.module.css', '.card { color: red; }'); | ||
| const name2 = generateScopedName('card', '/src/Card.module.css', '.card { color: blue; }'); | ||
| assert.equal(name1, name2, 'class name should be stable across CSS content changes'); | ||
|
|
||
| // Different files should produce different names for the same class | ||
| const nameA = generateScopedName('card', '/src/A.module.css', '.card { color: red; }'); | ||
| const nameB = generateScopedName('card', '/src/B.module.css', '.card { color: red; }'); | ||
| assert.notEqual(nameA, nameB, 'different files should produce different class names'); | ||
|
|
||
| // Different class names in the same file should produce different names | ||
| const nameCard = generateScopedName('card', '/src/Card.module.css', ''); | ||
| const nameTitle = generateScopedName('title', '/src/Card.module.css', ''); | ||
| assert.notEqual( | ||
| nameCard, | ||
| nameTitle, | ||
| 'different classes in the same file should produce different names', | ||
| ); | ||
|
|
||
| // Name should include the local class name for debuggability | ||
| assert.ok(name1.includes('card'), 'generated name should include the local class name'); | ||
| }); | ||
|
|
||
| it('does not set generateScopedName in build mode', async () => { | ||
| const settings = await createBasicSettings(); | ||
| const routesList = { routes: [] }; | ||
| const config = await createVite( | ||
| {}, | ||
| { | ||
| settings, | ||
| logger: defaultLogger, | ||
| mode: 'production', | ||
| command: 'build', | ||
| routesList, | ||
| sync: false, | ||
| }, | ||
| ); | ||
|
|
||
| const modules = config.css?.modules; | ||
| const generateScopedName = modules | ||
| ? (modules as { generateScopedName?: GenerateScopedName }).generateScopedName | ||
| : undefined; | ||
|
|
||
| assert.equal( | ||
| generateScopedName, | ||
| undefined, | ||
| 'build config should not override generateScopedName', | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.