Commit 85bb8728 authored by Zack Tanner's avatar Zack Tanner

fix: ensure route handlers properly track dynamic access (#66446)

In #60645, dynamic access tracking was refactored but we erroneously
stopped tracking dynamic access in route handlers. Request proxying, as
well as tracking segment level configs (such as `export const dynamic =
'force-dynamic'`), were only enabled during static generation. This was
an unintended breaking change that consequently caused dynamic access to
not properly bail from data cache in various circumstances.

This adds some more rigorous testing for route handlers, as this seems
to be a fairly large gap in our fetch cache testing currently.

This PR is easiest to review with [whitespace
disabled](https://github.com/vercel/next.js/pull/66446/files?w=1).
parent 2e7a96a1
......@@ -146,7 +146,7 @@ export function trackDynamicDataAccessed(
if (store.isStaticGeneration) {
// We aren't prerendering but we are generating a static page. We need to bail out of static generation
const err = new DynamicServerError(
`Route ${pathname} couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
`Route ${pathname} couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
)
store.dynamicUsageDescription = expression
store.dynamicUsageStack = err.stack
......
......@@ -840,6 +840,8 @@ createNextDescribe(
"variable-revalidate/authorization.rsc",
"variable-revalidate/authorization/page.js",
"variable-revalidate/authorization/page_client-reference-manifest.js",
"variable-revalidate/authorization/route-cookies/route.js",
"variable-revalidate/authorization/route-request/route.js",
"variable-revalidate/cookie.html",
"variable-revalidate/cookie.rsc",
"variable-revalidate/cookie/page.js",
......@@ -2456,6 +2458,38 @@ createNextDescribe(
}, 'success')
})
it('should skip fetch cache when an authorization header is present after dynamic usage', async () => {
const initialReq = await next.fetch(
'/variable-revalidate/authorization/route-cookies'
)
const initialJson = await initialReq.json()
await retry(async () => {
const req = await next.fetch(
'/variable-revalidate/authorization/route-cookies'
)
const json = await req.json()
expect(json).not.toEqual(initialJson)
})
})
it('should skip fetch cache when accessing request properties', async () => {
const initialReq = await next.fetch(
'/variable-revalidate/authorization/route-request'
)
const initialJson = await initialReq.json()
await retry(async () => {
const req = await next.fetch(
'/variable-revalidate/authorization/route-request'
)
const json = await req.json()
expect(json).not.toEqual(initialJson)
})
})
it('should not cache correctly with POST method request init', async () => {
const res = await fetchViaHTTP(
next.url,
......
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'
export async function GET() {
const cookieData = cookies()
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: {
Authorization: 'Bearer token',
},
}
).then((res) => res.text())
console.log('has cookie data', !!cookieData)
return NextResponse.json({ random: data })
}
import { NextResponse } from 'next/server'
export async function GET(request) {
const requestCookies = request.cookies
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: {
Authorization: 'Bearer token',
},
}
).then((res) => res.text())
console.log('has cookie data', !!requestCookies)
return NextResponse.json({ random: data })
}
......@@ -163,10 +163,10 @@ createNextDescribe(
if (!isNextDev) {
it('should track dynamic apis when rendering app routes', async () => {
expect(next.cliOutput).toContain(
`Caught Error: Dynamic server usage: Route /routes/url couldn't be rendered statically because it accessed \`request.url\`.`
`Caught Error: Dynamic server usage: Route /routes/url couldn't be rendered statically because it used \`request.url\`.`
)
expect(next.cliOutput).toContain(
`Caught Error: Dynamic server usage: Route /routes/next-url couldn't be rendered statically because it accessed \`nextUrl.toString\`.`
`Caught Error: Dynamic server usage: Route /routes/next-url couldn't be rendered statically because it used \`nextUrl.toString\`.`
)
})
}
......@@ -239,10 +239,10 @@ createNextDescribe(
'Error: Route /search with `dynamic = "error"` couldn\'t be rendered statically because it used `searchParams`.'
)
expect(next.cliOutput).toMatch(
'Error: Route /routes/form-data/error with `dynamic = "error"` couldn\'t be rendered statically because it accessed `request.formData`.'
'Error: Route /routes/form-data/error with `dynamic = "error"` couldn\'t be rendered statically because it used `request.formData`.'
)
expect(next.cliOutput).toMatch(
'Error: Route /routes/next-url/error with `dynamic = "error"` couldn\'t be rendered statically because it accessed `nextUrl.toString`.'
'Error: Route /routes/next-url/error with `dynamic = "error"` couldn\'t be rendered statically because it used `nextUrl.toString`.'
)
})
}
......
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