Skip to content

Commit

Permalink
Update fetch dynamic error handling (vercel#50822)
Browse files Browse the repository at this point in the history
Updates to not throw the dynamic server error inside of the patched
fetch as it's typical for fetch errors to be caught and handled and this
error is not meant to be caught by users. This instead throws it during
rendering so we can ensure we catch it instead of users.

x-ref:
https://github.com/vercel/next.js/actions/runs/5182384027/jobs/9339123096
  • Loading branch information
ijjk authored and hydRAnger committed Jun 12, 2023
1 parent 7e4d2ce commit 972475c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AsyncLocalStorage } from 'async_hooks'
import type { IncrementalCache } from '../../server/lib/incremental-cache'
import type { DynamicServerError } from './hooks-server-context'
import { createAsyncLocalStorage } from './async-local-storage'

export interface StaticGenerationStore {
Expand Down Expand Up @@ -27,6 +28,7 @@ export interface StaticGenerationStore {

dynamicUsageDescription?: string
dynamicUsageStack?: string
dynamicUsageErr?: DynamicServerError

nextFetchId?: number
pathWasRevalidated?: boolean
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,10 @@ export async function renderToHTMLOrFlight(
}
}

if (staticGenerationStore?.dynamicUsageErr) {
throw staticGenerationStore.dynamicUsageErr
}

/**
* The React Component to render.
*/
Expand Down
8 changes: 2 additions & 6 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,18 +480,15 @@ export function patchFetch({
}
if (cache === 'no-store') {
staticGenerationStore.revalidate = 0
// TODO: ensure this error isn't logged to the user
// seems it's slipping through currently
const dynamicUsageReason = `no-store fetch ${input}${
staticGenerationStore.pathname
? ` ${staticGenerationStore.pathname}`
: ''
}`
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason

throw err
}

const hasNextConfig = 'next' in init
Expand All @@ -516,11 +513,10 @@ export function patchFetch({
: ''
}`
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription =
dynamicUsageReason

throw err
}
}
if (hasNextConfig) delete init.next
Expand Down
1 change: 0 additions & 1 deletion test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ createNextDescribe(
files: __dirname,
env: {
NEXT_DEBUG_BUILD: '1',
NEXT_PRIVATE_DEBUG_CACHE: '1',
...(process.env.CUSTOM_CACHE_HANDLER
? {
CUSTOM_CACHE_HANDLER: process.env.CUSTOM_CACHE_HANDLER,
Expand Down

0 comments on commit 972475c

Please sign in to comment.