diff --git a/packages/next/src/server/lib/router-server.ts b/packages/next/src/server/lib/router-server.ts index 3005873561e006..1bd71496b4e7e5 100644 --- a/packages/next/src/server/lib/router-server.ts +++ b/packages/next/src/server/lib/router-server.ts @@ -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'), @@ -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( diff --git a/test/e2e/next-font/index.test.ts b/test/e2e/next-font/index.test.ts index 2299a910d6d66d..626b71a5c2bd4e 100644 --- a/test/e2e/next-font/index.test.ts +++ b/test/e2e/next-font/index.test.ts @@ -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') diff --git a/test/turbopack-tests-manifest.json b/test/turbopack-tests-manifest.json index 48c570eaa72dce..a20cfca7fbe1dc 100644 --- a/test/turbopack-tests-manifest.json +++ b/test/turbopack-tests-manifest.json @@ -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", @@ -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": [],