Unverified Commit 2eeb0c7f authored by Shu Ding's avatar Shu Ding Committed by GitHub

Add missing `module.hot.accept` call to server CSS modules (#47913)

Since we compile global server CSS imports into a special module with a
checksum of the original content, it should always accept HMR updates.
This fixes `Fast Refresh had to perform a full reload` errors.
parent a3acbf4f
...@@ -34,12 +34,20 @@ const NextServerCSSLoader = function (this: any, content: string) { ...@@ -34,12 +34,20 @@ const NextServerCSSLoader = function (this: any, content: string) {
.substring(0, 12) .substring(0, 12)
if (isCSSModule) { if (isCSSModule) {
return ( return `\
content + '\nmodule.exports.__checksum = ' + JSON.stringify(checksum) ${content}
) module.exports.__checksum = ${JSON.stringify(checksum)}
`
} }
return `export default ${JSON.stringify(checksum)}` // Server CSS imports are always available for HMR, so we attach
// `module.hot.accept()` to the generated module.
const hmrCode = 'if (module.hot) { module.hot.accept() }'
return `\
export default ${JSON.stringify(checksum)}
${hmrCode}
`
} }
return content return content
......
import './global.css'
export default function Page() {
return <div>hello!</div>
}
...@@ -496,6 +496,26 @@ createNextDescribe( ...@@ -496,6 +496,26 @@ createNextDescribe(
await next.patchFile(filePath, origContent) await next.patchFile(filePath, origContent)
} }
}) })
it('should not break HMR when CSS is imported in a server component', async () => {
const filePath = 'app/hmr/page.js'
const origContent = await next.readFile(filePath)
const browser = await next.browser('/hmr')
await browser.eval(`window.__v = 1`)
try {
await next.patchFile(
filePath,
origContent.replace('hello!', 'hmr!')
)
await check(() => browser.elementByCss('body').text(), 'hmr!')
// Make sure it doesn't reload the page
expect(await browser.eval(`window.__v`)).toBe(1)
} finally {
await next.patchFile(filePath, origContent)
}
})
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment