Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added cache control headers for static app routes #72521

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3292,7 +3292,10 @@ export default abstract class Server<

// If cache control is already set on the response we don't
// override it to allow users to customize it via next.config
if (cacheEntry.revalidate && !res.getHeader('Cache-Control')) {
if (
typeof cacheEntry.revalidate !== 'undefined' &&
!res.getHeader('Cache-Control')
) {
res.setHeader(
'Cache-Control',
formatRevalidate({
Expand All @@ -3315,7 +3318,10 @@ export default abstract class Server<
} else if (cachedData.kind === CachedRouteKind.REDIRECT) {
// If cache control is already set on the response we don't
// override it to allow users to customize it via next.config
if (cacheEntry.revalidate && !res.getHeader('Cache-Control')) {
if (
typeof cacheEntry.revalidate !== 'undefined' &&
!res.getHeader('Cache-Control')
) {
res.setHeader(
'Cache-Control',
formatRevalidate({
Expand All @@ -3339,17 +3345,33 @@ export default abstract class Server<
return null
}
} else if (cachedData.kind === CachedRouteKind.APP_ROUTE) {
const headers = { ...cachedData.headers }
const headers = fromNodeOutgoingHttpHeaders(cachedData.headers)

if (!(this.minimalMode && isSSG)) {
delete headers[NEXT_CACHE_TAGS_HEADER]
headers.delete(NEXT_CACHE_TAGS_HEADER)
}

// If cache control is already set on the response we don't
// override it to allow users to customize it via next.config
if (
typeof cacheEntry.revalidate !== 'undefined' &&
!res.getHeader('Cache-Control') &&
!headers.get('Cache-Control')
) {
headers.set(
'Cache-Control',
formatRevalidate({
revalidate: cacheEntry.revalidate,
expireTime: this.nextConfig.expireTime,
})
)
}

await sendResponse(
req,
res,
new Response(cachedData.body, {
headers: fromNodeOutgoingHttpHeaders(headers),
headers,
status: cachedData.status || 200,
})
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const dynamic = 'force-static'

export async function GET(request, context) {
const { params } = context
const { slug } = params
return Response.json({ message: `Hello, ${slug}!` })
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const revalidate = 3600

export default function Page() {
return <div>Hello, World!</div>
}

export async function generateStaticParams() {
return []
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ describe('required server files app router', () => {
if (server) await killApp(server)
})

it('should send the right cache headers for an app route', async () => {
const res = await fetchViaHTTP(appPort, '/api/test/123', undefined, {
headers: {
'x-matched-path': '/api/test/[slug]',
'x-now-route-matches': '1=123&nxtPslug=123',
},
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=31536000, stale-while-revalidate'
)
})

it('should send the right cache headers for an app page', async () => {
const res = await fetchViaHTTP(appPort, '/test/123', undefined, {
headers: {
'x-matched-path': '/test/[slug]',
'x-now-route-matches': '1=123&nxtPslug=123',
},
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=3600, stale-while-revalidate'
)
})

it('should not fail caching', async () => {
expect(next.cliOutput).not.toContain('ERR_INVALID_URL')
})
Expand Down
2 changes: 2 additions & 0 deletions test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16578,6 +16578,8 @@
"test/production/standalone-mode/required-server-files/required-server-files-app.test.ts": {
"passed": [],
"failed": [
"required server files app router should send the right cache headers for an app route",
"required server files app router should send the right cache headers for an app page",
"required server files app router should not fail caching",
"required server files app router should not send cache tags in minimal mode for SSR",
"required server files app router should not send invalid soft tags to cache handler",
Expand Down
Loading