Unverified Commit 9f248400 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix resource being preloaded multiple times during development (#53525)

Closes #49607. Since `ReactDOM.preload`s might be called multiple times during the rendering process, we need to ensure the timestamp queries are stable across the request so Flight can properly deduplicate them.
parent 0ecde6bd
...@@ -210,6 +210,12 @@ export async function renderToHTMLOrFlight( ...@@ -210,6 +210,12 @@ export async function renderToHTMLOrFlight(
const isFlight = req.headers[RSC.toLowerCase()] !== undefined const isFlight = req.headers[RSC.toLowerCase()] !== undefined
const pathname = validateURL(req.url) const pathname = validateURL(req.url)
// A unique request timestamp used by development to ensure that it's
// consistent and won't change during this request. This is important to
// avoid that resources can be deduped by React Float if the same resource is
// rendered or preloaded multiple times: `<link href="a.css?v={Date.now()}"/>`.
const DEV_REQUEST_TS = Date.now()
const { const {
buildManifest, buildManifest,
subresourceIntegrityManifest, subresourceIntegrityManifest,
...@@ -427,7 +433,7 @@ export async function renderToHTMLOrFlight( ...@@ -427,7 +433,7 @@ export async function renderToHTMLOrFlight(
let qs = '' let qs = ''
if (isDev && addTimestamp) { if (isDev && addTimestamp) {
qs += `?v=${Date.now()}` qs += `?v=${DEV_REQUEST_TS}`
} }
if (renderOpts.deploymentId) { if (renderOpts.deploymentId) {
......
...@@ -343,6 +343,38 @@ createNextDescribe( ...@@ -343,6 +343,38 @@ createNextDescribe(
} }
}) })
it('should not preload styles twice during HMR', async () => {
const filePath = 'app/hmr/page.js'
const origContent = await next.readFile(filePath)
const browser = await next.browser('/hmr')
try {
await next.patchFile(
filePath,
origContent.replace(
'<div>hello!</div>',
'<div>hello world!</div>'
)
)
// Wait for HMR to trigger
await check(
() => browser.elementByCss('body').text(),
'hello world!'
)
// there should be only 1 preload link
expect(
await browser.eval(
`document.querySelectorAll("link[rel=preload][href^='/_next/static/css/app/layout.css']").length`
)
).toBe(1)
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should reload @import styles during HMR', async () => { it('should reload @import styles during HMR', async () => {
const filePath = 'app/hmr/import/actual-styles.css' const filePath = 'app/hmr/import/actual-styles.css'
const origContent = await next.readFile(filePath) const origContent = await next.readFile(filePath)
......
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