Unverified Commit 0770f285 authored by David Alexandru Ilie's avatar David Alexandru Ilie Committed by GitHub

Stop app route prerenders after caught request access

parent a6913835
...@@ -637,10 +637,13 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -637,10 +637,13 @@ export class AppRouteRouteModule extends RouteModule<
trackPendingModules(cacheSignal) trackPendingModules(cacheSignal)
await cacheSignal.cacheReady() await cacheSignal.cacheReady()
if (prospectiveRenderIsDynamic) { // The handler may catch the prerender interruption and resolve
// normally, but dynamic tracking still records the request access.
const dynamicReason = getFirstDynamicReason(dynamicTracking)
if (prospectiveRenderIsDynamic || dynamicReason) {
// the route handler called an API which is always dynamic // the route handler called an API which is always dynamic
// there is no need to try again // there is no need to try again
const dynamicReason = getFirstDynamicReason(dynamicTracking)
if (dynamicReason) { if (dynamicReason) {
throw new DynamicServerError( throw new DynamicServerError(
`Route ${workStore.route} couldn't be rendered statically because it used \`${dynamicReason}\`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error` `Route ${workStore.route} couldn't be rendered statically because it used \`${dynamicReason}\`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
......
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../getSentinelValue'
export async function GET(request: NextRequest) {
let search = ''
try {
search = request.nextUrl.search
} catch {
console.log('caught dynamic URL access')
}
return new Response(
JSON.stringify({
value: getSentinelValue(),
search,
})
)
}
...@@ -50,6 +50,22 @@ describe('cache-components', () => { ...@@ -50,6 +50,22 @@ describe('cache-components', () => {
expect(json.search).toEqual('?foo=bar') expect(json.search).toEqual('?foo=bar')
}) })
it('should stop the prospective render when a dynamic API error is caught', async () => {
if (!isNextDev) {
const prospectiveRenderLogs = next.cliOutput
.split('\n')
.filter((line) => line.includes('caught dynamic URL access'))
expect(prospectiveRenderLogs).toHaveLength(1)
}
const str = await next.render('/routes/caught-dynamic-url?foo=bar', {})
const json = JSON.parse(str)
expect(json.value).toEqual('at runtime')
expect(json.search).toEqual('?foo=bar')
})
it('should prerender GET route handlers that have entirely cached io (fetches)', async () => { it('should prerender GET route handlers that have entirely cached io (fetches)', async () => {
let str = await next.render('/routes/fetch-cached', {}) let str = await next.render('/routes/fetch-cached', {})
let json = JSON.parse(str) let json = JSON.parse(str)
......
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