Unverified Commit d8e1aca8 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[metadata] update metadata routes cache headers (#83215)

parent 15a05670
...@@ -112,7 +112,6 @@ pub async fn get_app_metadata_route_entry( ...@@ -112,7 +112,6 @@ pub async fn get_app_metadata_route_entry(
} }
const CACHE_HEADER_NONE: &str = "no-cache, no-store"; const CACHE_HEADER_NONE: &str = "no-cache, no-store";
const CACHE_HEADER_LONG_CACHE: &str = "public, immutable, no-transform, max-age=31536000";
const CACHE_HEADER_REVALIDATE: &str = "public, max-age=0, must-revalidate"; const CACHE_HEADER_REVALIDATE: &str = "public, max-age=0, must-revalidate";
async fn get_base64_file_content(path: FileSystemPath) -> Result<String> { async fn get_base64_file_content(path: FileSystemPath) -> Result<String> {
...@@ -137,10 +136,8 @@ async fn static_route_source(mode: NextMode, path: FileSystemPath) -> Result<Vc< ...@@ -137,10 +136,8 @@ async fn static_route_source(mode: NextMode, path: FileSystemPath) -> Result<Vc<
let stem = path.file_stem(); let stem = path.file_stem();
let stem = stem.unwrap_or_default(); let stem = stem.unwrap_or_default();
let cache_control = if stem == "favicon" { let cache_control = if mode.is_production() {
CACHE_HEADER_REVALIDATE CACHE_HEADER_REVALIDATE
} else if mode.is_production() {
CACHE_HEADER_LONG_CACHE
} else { } else {
CACHE_HEADER_NONE CACHE_HEADER_NONE
}; };
......
...@@ -43,11 +43,7 @@ async function nextMetadataImageLoader( ...@@ -43,11 +43,7 @@ async function nextMetadataImageLoader(
const opts = { context, content } const opts = { context, content }
// No hash query for favicon.ico const contentHash = loaderUtils.interpolateName(this, '[contenthash]', opts)
const contentHash =
type === 'favicon'
? ''
: loaderUtils.interpolateName(this, '[contenthash]', opts)
const interpolatedName = loaderUtils.interpolateName( const interpolatedName = loaderUtils.interpolateName(
this, this,
...@@ -179,7 +175,7 @@ async function nextMetadataImageLoader( ...@@ -179,7 +175,7 @@ async function nextMetadataImageLoader(
return [{ return [{
...imageData, ...imageData,
url: imageUrl + ${JSON.stringify(type === 'favicon' ? '' : hashQuery)}, url: imageUrl + ${JSON.stringify(hashQuery)},
}] }]
}` }`
} }
......
...@@ -35,10 +35,9 @@ async function createReExportsCode( ...@@ -35,10 +35,9 @@ async function createReExportsCode(
: '' : ''
} }
const cacheHeader = { const CACHE_HEADERS = {
none: 'no-cache, no-store', NO_CACHE: 'no-cache, no-store',
longCache: 'public, immutable, no-transform, max-age=31536000', REVALIDATE: 'public, max-age=0, must-revalidate',
revalidate: 'public, max-age=0, must-revalidate',
} }
export type MetadataRouteLoaderOptions = { export type MetadataRouteLoaderOptions = {
...@@ -77,11 +76,9 @@ async function getStaticAssetRouteCode( ...@@ -77,11 +76,9 @@ async function getStaticAssetRouteCode(
fileBaseName: string fileBaseName: string
) { ) {
const cache = const cache =
fileBaseName === 'favicon' process.env.NODE_ENV !== 'production'
? 'public, max-age=0, must-revalidate' ? CACHE_HEADERS.NO_CACHE
: process.env.NODE_ENV !== 'production' : CACHE_HEADERS.REVALIDATE
? cacheHeader.none
: cacheHeader.longCache
const isTwitter = fileBaseName === 'twitter-image' const isTwitter = fileBaseName === 'twitter-image'
const isOpenGraph = fileBaseName === 'opengraph-image' const isOpenGraph = fileBaseName === 'opengraph-image'
...@@ -149,7 +146,7 @@ export async function GET() { ...@@ -149,7 +146,7 @@ export async function GET() {
return new NextResponse(content, { return new NextResponse(content, {
headers: { headers: {
'Content-Type': contentType, 'Content-Type': contentType,
'Cache-Control': ${JSON.stringify(cacheHeader.revalidate)}, 'Cache-Control': ${JSON.stringify(CACHE_HEADERS.REVALIDATE)},
}, },
}) })
} }
...@@ -271,7 +268,7 @@ export async function GET(_, ctx) { ...@@ -271,7 +268,7 @@ export async function GET(_, ctx) {
return new NextResponse(content, { return new NextResponse(content, {
headers: { headers: {
'Content-Type': contentType, 'Content-Type': contentType,
'Cache-Control': ${JSON.stringify(cacheHeader.revalidate)}, 'Cache-Control': ${JSON.stringify(CACHE_HEADERS.REVALIDATE)},
}, },
}) })
} }
......
...@@ -49,7 +49,7 @@ export class ImageResponse extends Response { ...@@ -49,7 +49,7 @@ export class ImageResponse extends Response {
'cache-control': 'cache-control':
process.env.NODE_ENV === 'development' process.env.NODE_ENV === 'development'
? 'no-cache, no-store' ? 'no-cache, no-store'
: 'public, immutable, no-transform, max-age=31536000', : 'public, max-age=0, must-revalidate',
}) })
if (options.headers) { if (options.headers) {
const newHeaders = new Headers(options.headers) const newHeaders = new Headers(options.headers)
......
...@@ -4,7 +4,6 @@ import { check } from 'next-test-utils' ...@@ -4,7 +4,6 @@ import { check } from 'next-test-utils'
const CACHE_HEADERS = { const CACHE_HEADERS = {
NONE: 'no-cache, no-store', NONE: 'no-cache, no-store',
LONG: 'public, immutable, no-transform, max-age=31536000',
REVALIDATE: 'public, max-age=0, must-revalidate', REVALIDATE: 'public, max-age=0, must-revalidate',
} }
...@@ -209,7 +208,7 @@ describe('app dir - metadata dynamic routes', () => { ...@@ -209,7 +208,7 @@ describe('app dir - metadata dynamic routes', () => {
expect(res.headers.get('content-type')).toBe('image/png') expect(res.headers.get('content-type')).toBe('image/png')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.LONG isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.REVALIDATE
) )
}) })
...@@ -219,7 +218,7 @@ describe('app dir - metadata dynamic routes', () => { ...@@ -219,7 +218,7 @@ describe('app dir - metadata dynamic routes', () => {
expect(res.headers.get('content-type')).toBe('image/png') expect(res.headers.get('content-type')).toBe('image/png')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.LONG isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.REVALIDATE
) )
if (isNextDev) { if (isNextDev) {
...@@ -241,7 +240,7 @@ describe('app dir - metadata dynamic routes', () => { ...@@ -241,7 +240,7 @@ describe('app dir - metadata dynamic routes', () => {
res = await next.fetch('/twitter-image2') res = await next.fetch('/twitter-image2')
expect(res.headers.get('content-type')).toBe('image/png') expect(res.headers.get('content-type')).toBe('image/png')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.LONG isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.REVALIDATE
) )
}) })
...@@ -338,7 +337,7 @@ describe('app dir - metadata dynamic routes', () => { ...@@ -338,7 +337,7 @@ describe('app dir - metadata dynamic routes', () => {
expect(res.headers.get('content-type')).toBe('image/png') expect(res.headers.get('content-type')).toBe('image/png')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.LONG isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.REVALIDATE
) )
}) })
...@@ -347,7 +346,7 @@ describe('app dir - metadata dynamic routes', () => { ...@@ -347,7 +346,7 @@ describe('app dir - metadata dynamic routes', () => {
expect(res.headers.get('content-type')).toBe('image/png') expect(res.headers.get('content-type')).toBe('image/png')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.LONG isNextDev ? CACHE_HEADERS.NONE : CACHE_HEADERS.REVALIDATE
) )
}) })
}) })
......
...@@ -12,6 +12,10 @@ import { ...@@ -12,6 +12,10 @@ import {
import fs from 'fs/promises' import fs from 'fs/promises'
import path from 'path' import path from 'path'
// Webpack: /favicon.ico?<hash>
// Turbopack: /favicon.ico?favicon.<hash>.ico
const FAVICON_REGEX = /\/favicon.ico\?\w+/
describe('app dir - metadata', () => { describe('app dir - metadata', () => {
const { next, isNextDev, isNextStart, isNextDeploy } = nextTestSetup({ const { next, isNextDev, isNextStart, isNextDeploy } = nextTestSetup({
files: __dirname, files: __dirname,
...@@ -428,7 +432,7 @@ describe('app dir - metadata', () => { ...@@ -428,7 +432,7 @@ describe('app dir - metadata', () => {
}) })
// favicon shouldn't be overridden // favicon shouldn't be overridden
expect($('link[rel="icon"]').attr('href')).toMatch('/favicon.ico') expect($('link[rel="icon"]').attr('href')).toMatch(FAVICON_REGEX)
}) })
it('should override file based images when opengraph-image and twitter-image specify images property', async () => { it('should override file based images when opengraph-image and twitter-image specify images property', async () => {
...@@ -450,7 +454,7 @@ describe('app dir - metadata', () => { ...@@ -450,7 +454,7 @@ describe('app dir - metadata', () => {
.toArray() .toArray()
.map((i) => $(i).attr('href')) .map((i) => $(i).attr('href'))
expect(favicon).toMatch('/favicon.ico') expect(favicon).toMatch(FAVICON_REGEX)
expect(icons).toEqual(['https://custom-icon-1.png']) expect(icons).toEqual(['https://custom-icon-1.png'])
}) })
...@@ -485,7 +489,7 @@ describe('app dir - metadata', () => { ...@@ -485,7 +489,7 @@ describe('app dir - metadata', () => {
await checkLink(browser, 'shortcut icon', '/shortcut-icon.png') await checkLink(browser, 'shortcut icon', '/shortcut-icon.png')
await checkLink(browser, 'icon', [ await checkLink(browser, 'icon', [
expect.stringMatching(/favicon\.ico/), expect.stringMatching(FAVICON_REGEX),
'/icon.png', '/icon.png',
'https://example.com/icon.png', 'https://example.com/icon.png',
]) ])
...@@ -524,7 +528,7 @@ describe('app dir - metadata', () => { ...@@ -524,7 +528,7 @@ describe('app dir - metadata', () => {
it('should support root level of favicon.ico', async () => { it('should support root level of favicon.ico', async () => {
let $ = await next.render$('/') let $ = await next.render$('/')
const favIcon = $('link[rel="icon"]') const favIcon = $('link[rel="icon"]')
expect(favIcon.attr('href')).toMatch('/favicon.ico') expect(favIcon.attr('href')).toMatch(FAVICON_REGEX)
expect(favIcon.attr('type')).toBe('image/x-icon') expect(favIcon.attr('type')).toBe('image/x-icon')
// Turbopack renders / emits image differently // Turbopack renders / emits image differently
expect(['16x16', '48x48']).toContain(favIcon.attr('sizes')) expect(['16x16', '48x48']).toContain(favIcon.attr('sizes'))
...@@ -536,7 +540,7 @@ describe('app dir - metadata', () => { ...@@ -536,7 +540,7 @@ describe('app dir - metadata', () => {
$ = await next.render$('/basic') $ = await next.render$('/basic')
const icon = $('link[rel="icon"]') const icon = $('link[rel="icon"]')
expect(icon.attr('href')).toMatch('/favicon.ico') expect(icon.attr('href')).toMatch(FAVICON_REGEX)
expect(['16x16', '48x48']).toContain(favIcon.attr('sizes')) expect(['16x16', '48x48']).toContain(favIcon.attr('sizes'))
if (!isNextDeploy) { if (!isNextDeploy) {
...@@ -676,7 +680,7 @@ describe('app dir - metadata', () => { ...@@ -676,7 +680,7 @@ describe('app dir - metadata', () => {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('image/x-icon') expect(res.headers.get('content-type')).toBe('image/x-icon')
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
'public, max-age=0, must-revalidate' isNextDev ? 'no-cache, no-store' : 'public, max-age=0, must-revalidate'
) )
}) })
...@@ -689,20 +693,12 @@ describe('app dir - metadata', () => { ...@@ -689,20 +693,12 @@ describe('app dir - metadata', () => {
expect(resAppleIcon.status).toBe(200) expect(resAppleIcon.status).toBe(200)
expect(resAppleIcon.headers.get('content-type')).toBe('image/png') expect(resAppleIcon.headers.get('content-type')).toBe('image/png')
expect(resAppleIcon.headers.get('cache-control')).toBe( expect(resAppleIcon.headers.get('cache-control')).toBe(
isNextDev isNextDev ? 'no-cache, no-store' : 'public, max-age=0, must-revalidate'
? 'no-cache, no-store'
: isNextDeploy
? 'public, max-age=0, must-revalidate'
: 'public, immutable, no-transform, max-age=31536000'
) )
expect(resIcon.status).toBe(200) expect(resIcon.status).toBe(200)
expect(resIcon.headers.get('content-type')).toBe('image/png') expect(resIcon.headers.get('content-type')).toBe('image/png')
expect(resIcon.headers.get('cache-control')).toBe( expect(resIcon.headers.get('cache-control')).toBe(
isNextDev isNextDev ? 'no-cache, no-store' : 'public, max-age=0, must-revalidate'
? 'no-cache, no-store'
: isNextDeploy
? 'public, max-age=0, must-revalidate'
: 'public, immutable, no-transform, max-age=31536000'
) )
}) })
......
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