Skip to content
Draft
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
10 changes: 9 additions & 1 deletion packages/next/src/server/route-modules/app-route/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,15 @@ export class AppRouteRouteModule extends RouteModule<
trackPendingModules(cacheSignal)
await cacheSignal.cacheReady()

if (prospectiveRenderIsDynamic) {
// The handler may catch the prerender interruption and resolve
// normally, but dynamic tracking still records the request access.
const hasDynamicRequestAccess = dynamicTracking.dynamicAccesses.some(
({ expression }) =>
expression.startsWith('nextUrl.') ||
expression.startsWith('request.')
)

if (prospectiveRenderIsDynamic || hasDynamicRequestAccess) {
// the route handler called an API which is always dynamic
// there is no need to try again
const dynamicReason = getFirstDynamicReason(dynamicTracking)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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,
})
)
}
16 changes: 16 additions & 0 deletions test/e2e/app-dir/cache-components/cache-components.routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ describe('cache-components', () => {
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 () => {
let str = await next.render('/routes/fetch-cached', {})
let json = JSON.parse(str)
Expand Down
Loading