Skip to content

Commit

Permalink
fix: cache next font during development to avoid FOUC (vercel#60175)
Browse files Browse the repository at this point in the history
In fact, this is an issue that has been solved in vercel#52033, but it seems
vercel#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 vercel#50782

---------

Co-authored-by: Tim Neutkens <[email protected]>
  • Loading branch information
2 people authored and agustints committed Jan 6, 2024
1 parent 77fd8a9 commit 10d5365
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { DevBundlerService } from './dev-bundler-service'
import { type Span, trace } from '../../trace'

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<
typeof import('./render-server'),
Expand Down Expand Up @@ -396,7 +398,7 @@ export async function initialize(opts: {
!res.getHeader('cache-control') &&
matchedOutput.type === 'nextStaticFolder'
) {
if (opts.dev) {
if (opts.dev && !isNextFont(parsedUrl.pathname)) {
res.setHeader('Cache-Control', 'no-store, must-revalidate')
} else {
res.setHeader(
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/next-font/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ describe('next/font', () => {
})
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', () => {
test('page with font', async () => {
const html = await renderViaHTTP(next.url, '/with-fonts')
Expand Down
4 changes: 3 additions & 1 deletion test/turbopack-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5582,6 +5582,7 @@
"next/font app preload page with fonts",
"next/font app preload page with local 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 Indie flower",
"next/font app-old Fallback fontfaces local Fraunces",
Expand All @@ -5594,7 +5595,8 @@
"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 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": [],
"flakey": [],
Expand Down

0 comments on commit 10d5365

Please sign in to comment.