-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(rsc): remove outdated stale hmr css references #7219
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
Open
jantimon
wants to merge
8
commits into
TanStack:main
Choose a base branch
from
jantimon:fix-rsc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
066c658
fix(rsc): remove outdated stale hmr css references
jantimon 94b1643
add e2e tests
jantimon 3ecc859
fix(e2e-rsc): preserve css side effects in rsc-hmr package
jantimon 72329f2
fix(e2e-rsc): order imports per lint rule in rsc-hmr-css spec
jantimon f4f9275
chore(e2e-rsc): sync lockfile with rsc-hmr package
jantimon 5fcaecd
fix(e2e-rsc): use top-level type import for playwright Page
jantimon a84966c
Merge remote-tracking branch 'origin/main' into fix-rsc
jantimon 3fa7ef9
chore: add changeset for rsc preinit fix
jantimon 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import ReactDOM from 'react-dom' | ||
|
|
||
| /** | ||
| * Vite's HMR cache-bust query, e.g. `?t=1776450256042` | ||
| * Vite only adds this during dev, never in a prod build | ||
| */ | ||
| const HMR_CACHE_BUST = /[?&]t=\d+/ | ||
|
|
||
| /** | ||
| * Tells React 19 Float to start fetching each CSS file early and put a | ||
| * <link rel="stylesheet" data-precedence="high"> in <head> during production builds | ||
| */ | ||
| export function preinitCssHrefs(cssHrefs: Iterable<string> | undefined): void { | ||
| if (!cssHrefs) return | ||
| for (const href of cssHrefs) { | ||
| // Skip Vite's HMR cache-bust query so that no stale <link>s pile up | ||
| // in <head> during dev from HMR edits. In prod builds, there are no `?t=` hrefs | ||
| if (HMR_CACHE_BUST.test(href)) continue | ||
| ReactDOM.preinit(href, { as: 'style', precedence: 'high' }) | ||
| } | ||
| } | ||
|
|
||
| export const _internals = { HMR_CACHE_BUST } | ||
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,94 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import ReactDOM from 'react-dom' | ||
|
|
||
| import { _internals, preinitCssHrefs } from '../src/preinitCssHrefs' | ||
|
|
||
| describe('HMR_CACHE_BUST', () => { | ||
| const { HMR_CACHE_BUST } = _internals | ||
|
|
||
| it.each([ | ||
| '/src/components/Card.css?t=1776450256042', | ||
| '/assets/app.css?v=abc&t=123', | ||
| '/a.css?t=0', | ||
| ])('matches Vite HMR cache-bust hrefs (%s)', (href) => { | ||
| expect(HMR_CACHE_BUST.test(href)).toBe(true) | ||
| }) | ||
|
|
||
| it.each([ | ||
| '/src/components/Card.css', | ||
| '/assets/app.css?v=abc', | ||
| '/a.Card.abc123.css', | ||
| '/a.css?theme=dark', | ||
| // `t=` without digits (not a Vite cache-bust) | ||
| '/a.css?t=abc', | ||
| // `t=` as a substring of a larger param name | ||
| '/a.css?format=3', | ||
| ])('does not match non-cache-busted hrefs (%s)', (href) => { | ||
| expect(HMR_CACHE_BUST.test(href)).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('preinitCssHrefs', () => { | ||
| let preinitSpy: ReturnType<typeof vi.spyOn> | ||
|
|
||
| beforeEach(() => { | ||
| preinitSpy = vi.spyOn(ReactDOM, 'preinit').mockImplementation(() => {}) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| preinitSpy.mockRestore() | ||
| }) | ||
|
|
||
| it('preinits non-cache-busted hrefs with {as:"style", precedence:"high"}', () => { | ||
| preinitCssHrefs(['/assets/app.abc123.css']) | ||
| expect(preinitSpy).toHaveBeenCalledTimes(1) | ||
| expect(preinitSpy).toHaveBeenCalledWith('/assets/app.abc123.css', { | ||
| as: 'style', | ||
| precedence: 'high', | ||
| }) | ||
| }) | ||
|
|
||
| it('skips hrefs matching the HMR cache-bust pattern', () => { | ||
| preinitCssHrefs([ | ||
| '/src/components/Card.css?t=1776450256042', | ||
| '/assets/app.css?v=abc&t=123', | ||
| ]) | ||
| expect(preinitSpy).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('preinits only the non-cache-busted subset from a mixed list', () => { | ||
| preinitCssHrefs([ | ||
| '/a.css?t=1', | ||
| '/b.Card.abc.css', | ||
| '/c.css?t=2', | ||
| '/d.css?theme=dark', | ||
| ]) | ||
| expect(preinitSpy).toHaveBeenCalledTimes(2) | ||
| expect(preinitSpy).toHaveBeenNthCalledWith(1, '/b.Card.abc.css', { | ||
| as: 'style', | ||
| precedence: 'high', | ||
| }) | ||
| expect(preinitSpy).toHaveBeenNthCalledWith(2, '/d.css?theme=dark', { | ||
| as: 'style', | ||
| precedence: 'high', | ||
| }) | ||
| }) | ||
|
|
||
| it.each([undefined, [], new Set<string>()])( | ||
| 'is a no-op for empty or missing input (%#)', | ||
| (input) => { | ||
| preinitCssHrefs(input) | ||
| expect(preinitSpy).not.toHaveBeenCalled() | ||
| }, | ||
| ) | ||
|
|
||
| it('accepts a ReadonlySet<string> (the actual call-site type)', () => { | ||
| const hrefs: ReadonlySet<string> = new Set(['/a.css?t=1', '/b.abc.css']) | ||
| preinitCssHrefs(hrefs) | ||
| expect(preinitSpy).toHaveBeenCalledTimes(1) | ||
| expect(preinitSpy).toHaveBeenCalledWith('/b.abc.css', { | ||
| as: 'style', | ||
| precedence: 'high', | ||
| }) | ||
| }) | ||
| }) |
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.