Skip to content

Commit

Permalink
fix: decrypt bound args before generating a cache key (#72463)
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce authored and wyattjoh committed Nov 9, 2024
1 parent 9666c48 commit b7c689c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
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 @@ -16629,6 +16629,8 @@
"test/production/standalone-mode/required-server-files/required-server-files-ppr.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 middleware rewrite should work with a dynamic path",
"required server files app router middleware rewrite should work with a dynamic path with Next-Resume",
"required server files app router should handle RSC requests",
Expand Down

0 comments on commit b7c689c

Please sign in to comment.