Skip to content

Commit 9231a86

Browse files
authoredDec 19, 2022
fix: don't invalidate when code is invalid (#67)
1 parent 9955e30 commit 9231a86

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed
 

Diff for: ‎packages/plugin-react/src/fast-refresh.ts

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function isReactRefreshBoundary(mod) {
9292
}
9393
9494
import.meta.hot.accept(mod => {
95+
if (!mod) return;
9596
if (isReactRefreshBoundary(mod)) {
9697
${timeout}
9798
} else {

Diff for: ‎playground/react/__tests__/react.spec.ts

+26-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,34 @@ test('should update', async () => {
1919
})
2020

2121
test('should hmr', async () => {
22-
editFile('App.jsx', (code) => code.replace('Vite + React', 'Updated'))
23-
await untilUpdated(() => page.textContent('h1'), 'Hello Updated')
22+
editFile('App.jsx', (code) =>
23+
code.replace('Vite + React', 'Vite + React Updated'),
24+
)
25+
await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React Updated')
2426
// preserve state
2527
expect(await page.textContent('#state-button')).toMatch('count is: 1')
28+
29+
editFile('App.jsx', (code) =>
30+
code.replace('Vite + React Updated', 'Vite + React'),
31+
)
32+
await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React')
33+
})
34+
35+
test.runIf(isServe)('should not invalidate when code is invalid', async () => {
36+
editFile('App.jsx', (code) =>
37+
code.replace('<div className="App">', '<div className="App"}>'),
38+
)
39+
40+
await untilUpdated(
41+
() => page.textContent('vite-error-overlay .message-body'),
42+
'Unexpected token',
43+
)
44+
// if import.meta.invalidate happened, the old page won't be shown because the page is reloaded
45+
expect(await page.textContent('h1')).toMatch('Hello Vite + React')
46+
47+
editFile('App.jsx', (code) =>
48+
code.replace('<div className="App"}>', '<div className="App">'),
49+
)
2650
})
2751

2852
test.runIf(isServe)(
@@ -59,7 +83,6 @@ if (!isBuild) {
5983
'[vite] hot updated: /hmr/parent.jsx',
6084
'Parent rendered',
6185
],
62-
true,
6386
)
6487
await untilUpdated(() => page.textContent('#parent'), 'Updated')
6588
})
@@ -86,7 +109,6 @@ if (!isBuild) {
86109
'[vite] hot updated: /context/ContextButton.jsx',
87110
'Parent rendered',
88111
],
89-
true,
90112
)
91113
await untilUpdated(
92114
() => page.textContent('#context-provider'),

0 commit comments

Comments
 (0)
Please sign in to comment.