Unverified Commit a2f4ca52 authored by pacexy's avatar pacexy Committed by GitHub

fix: cache next font during development to avoid FOUC (#60175)

In fact, this is an issue that has been solved in #52033, but it seems
#52492 introduced it again.

> 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.

Fixes #50782

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 28fdc367
...@@ -35,6 +35,8 @@ import { DevBundlerService } from './dev-bundler-service' ...@@ -35,6 +35,8 @@ import { DevBundlerService } from './dev-bundler-service'
import { type Span, trace } from '../../trace' import { type Span, trace } from '../../trace'
const debug = setupDebug('next:router-server:main') const debug = setupDebug('next:router-server:main')
const isNextFont = (pathname: string | null) =>
pathname && /\/media\/[^/]+\.(woff|woff2|eot|ttf|otf)$/.test(pathname)
export type RenderServer = Pick< export type RenderServer = Pick<
typeof import('./render-server'), typeof import('./render-server'),
...@@ -396,7 +398,7 @@ export async function initialize(opts: { ...@@ -396,7 +398,7 @@ export async function initialize(opts: {
!res.getHeader('cache-control') && !res.getHeader('cache-control') &&
matchedOutput.type === 'nextStaticFolder' matchedOutput.type === 'nextStaticFolder'
) { ) {
if (opts.dev) { if (opts.dev && !isNextFont(parsedUrl.pathname)) {
res.setHeader('Cache-Control', 'no-store, must-revalidate') res.setHeader('Cache-Control', 'no-store, must-revalidate')
} else { } else {
res.setHeader( res.setHeader(
......
...@@ -42,6 +42,20 @@ describe('next/font', () => { ...@@ -42,6 +42,20 @@ describe('next/font', () => {
}) })
afterAll(() => next.destroy()) afterAll(() => next.destroy())
if ((global as any).isNextDev) {
it('should use production cache control for fonts', async () => {
const html = await next.render('/')
console.log({ html })
const $ = await next.render$('/')
const link = $('[rel="preload"][as="font"]').attr('href')
expect(link).toBeDefined()
const res = await next.fetch(link)
expect(res.headers.get('cache-control')).toBe(
'public, max-age=31536000, immutable'
)
})
}
describe('import values', () => { describe('import values', () => {
test('page with font', async () => { test('page with font', async () => {
const html = await renderViaHTTP(next.url, '/with-fonts') const html = await renderViaHTTP(next.url, '/with-fonts')
......
...@@ -5582,6 +5582,7 @@ ...@@ -5582,6 +5582,7 @@
"next/font app preload page with fonts", "next/font app preload page with fonts",
"next/font app preload page with local fonts", "next/font app preload page with local fonts",
"next/font app preload page without fonts", "next/font app preload page without fonts",
"next/font app should use production cache control for fonts",
"next/font app-old Fallback fontfaces google Fraunces", "next/font app-old Fallback fontfaces google Fraunces",
"next/font app-old Fallback fontfaces google Indie flower", "next/font app-old Fallback fontfaces google Indie flower",
"next/font app-old Fallback fontfaces local Fraunces", "next/font app-old Fallback fontfaces local Fraunces",
...@@ -5594,7 +5595,8 @@ ...@@ -5594,7 +5595,8 @@
"next/font app-old preload google fonts with multiple weights/styles", "next/font app-old preload google fonts with multiple weights/styles",
"next/font app-old preload page with fonts", "next/font app-old preload page with fonts",
"next/font app-old preload page with local fonts", "next/font app-old preload page with local fonts",
"next/font app-old preload page without fonts" "next/font app-old preload page without fonts",
"next/font app-old should use production cache control for fonts"
], ],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
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