diff --git a/packages/plugin-react/src/fast-refresh.ts b/packages/plugin-react/src/fast-refresh.ts index 2dc7694fd..9503d5cee 100644 --- a/packages/plugin-react/src/fast-refresh.ts +++ b/packages/plugin-react/src/fast-refresh.ts @@ -92,6 +92,7 @@ function isReactRefreshBoundary(mod) { } import.meta.hot.accept(mod => { + if (!mod) return; if (isReactRefreshBoundary(mod)) { ${timeout} } else { diff --git a/playground/react/__tests__/react.spec.ts b/playground/react/__tests__/react.spec.ts index 58057b787..d9b4d01e4 100644 --- a/playground/react/__tests__/react.spec.ts +++ b/playground/react/__tests__/react.spec.ts @@ -19,10 +19,34 @@ test('should update', async () => { }) test('should hmr', async () => { - editFile('App.jsx', (code) => code.replace('Vite + React', 'Updated')) - await untilUpdated(() => page.textContent('h1'), 'Hello Updated') + editFile('App.jsx', (code) => + code.replace('Vite + React', 'Vite + React Updated'), + ) + await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React Updated') // preserve state expect(await page.textContent('#state-button')).toMatch('count is: 1') + + editFile('App.jsx', (code) => + code.replace('Vite + React Updated', 'Vite + React'), + ) + await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React') +}) + +test.runIf(isServe)('should not invalidate when code is invalid', async () => { + editFile('App.jsx', (code) => + code.replace('