Unverified Commit 636c7761 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix font missing during server HMR (#52033)

During development, for fonts created via `next/font` the file path is already containing the hash so we can always have them cached. This fixes the problem of fonts causing FOUC in HMR.

Since I can't find an easy way to test it reliably, I recorded two screenshots using the same steps as reproduced in #50879:

https://github.com/vercel/next.js/assets/3676859/5cb6a64c-cb4a-4ed4-808f-d7b2d0d6c0a3

With the fix:

https://github.com/vercel/next.js/assets/3676859/154b2332-a94b-4392-8132-4be609670745


fix #50879
fix NEXT-1334
parent 97461f0b
...@@ -25,7 +25,7 @@ const PLUGIN_NAME = 'NextFontManifestPlugin' ...@@ -25,7 +25,7 @@ const PLUGIN_NAME = 'NextFontManifestPlugin'
*/ */
function getPreloadedFontFiles(fontFiles: string[]) { function getPreloadedFontFiles(fontFiles: string[]) {
return fontFiles.filter((file: string) => return fontFiles.filter((file: string) =>
/\.p.(woff|woff2|eot|ttf|otf)$/.test(file) /\.p\.(woff|woff2|eot|ttf|otf)$/.test(file)
) )
} }
......
...@@ -37,7 +37,13 @@ export function linkGc() { ...@@ -37,7 +37,13 @@ export function linkGc() {
if (otherHref) { if (otherHref) {
const [, otherVersion] = otherHref.split('?v=') const [, otherVersion] = otherHref.split('?v=')
if (!otherVersion || +otherVersion < +version) { if (!otherVersion || +otherVersion < +version) {
otherLink.remove() // Delay the removal of the stylesheet to avoid FOUC
// caused by `@font-face` rules, as they seem to be
// a couple of ticks delayed between the old and new
// styles being swapped even if the font is cached.
setTimeout(() => {
otherLink.remove()
}, 5)
const preloadLink = document.querySelector( const preloadLink = document.querySelector(
`link[rel="preload"][as="style"][href="${otherHref}"]` `link[rel="preload"][as="style"][href="${otherHref}"]`
) )
......
...@@ -1793,7 +1793,21 @@ export default class DevServer extends Server { ...@@ -1793,7 +1793,21 @@ export default class DevServer extends Server {
return await loadDefaultErrorComponents(this.distDir) return await loadDefaultErrorComponents(this.distDir)
} }
protected setImmutableAssetCacheControl(res: BaseNextResponse): void { protected setImmutableAssetCacheControl(
res: BaseNextResponse,
pathSegments: string[]
): void {
// `next/font` generates checksum in the filepath even in dev,
// we can safely cache fonts to avoid FOUC of fonts during development.
if (
pathSegments[0] === 'media' &&
pathSegments[1] &&
/\.(woff|woff2|eot|ttf|otf)$/.test(pathSegments[1])
) {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
return
}
res.setHeader('Cache-Control', 'no-store, must-revalidate') res.setHeader('Cache-Control', 'no-store, must-revalidate')
} }
......
...@@ -625,7 +625,10 @@ export default class NextNodeServer extends BaseServer { ...@@ -625,7 +625,10 @@ export default class NextNodeServer extends BaseServer {
: [] : []
} }
protected setImmutableAssetCacheControl(res: BaseNextResponse): void { protected setImmutableAssetCacheControl(
res: BaseNextResponse,
_pathSegments: string[]
): void {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable') res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
} }
...@@ -654,7 +657,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -654,7 +657,7 @@ export default class NextNodeServer extends BaseServer {
params.path[0] === 'pages' || params.path[0] === 'pages' ||
params.path[1] === 'pages' params.path[1] === 'pages'
) { ) {
this.setImmutableAssetCacheControl(res) this.setImmutableAssetCacheControl(res, params.path)
} }
const p = join( const p = join(
this.distDir, this.distDir,
......
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