Skip to content

Commit

Permalink
Make notFound() work in "use cache" page
Browse files Browse the repository at this point in the history
When `notFound()` is called in a `"use cache"` function, we need to make
sure that the custom error digest is included in the serialized RSC
payload. Otherwise the `HTTPAccessFallbackErrorBoundary` won't be able
to detect the error as an `HTTPAccessFallbackError`.

fixes #73130
  • Loading branch information
unstubbable committed Nov 29, 2024
1 parent 3d82475 commit c37b182
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
12 changes: 6 additions & 6 deletions packages/next/src/server/app-render/create-error-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type SSRErrorHandler = (
export type DigestedError = Error & { digest: string }

export function createFlightReactServerErrorHandler(
dev: boolean,
shouldFormatError: boolean,
onReactServerRenderError: (err: DigestedError) => void
): RSCErrorHandler {
return (thrownValue: unknown) => {
Expand Down Expand Up @@ -56,7 +56,7 @@ export function createFlightReactServerErrorHandler(
}

// Format server errors in development to add more helpful error messages
if (dev) {
if (shouldFormatError) {
formatServerError(err)
}

Expand All @@ -77,7 +77,7 @@ export function createFlightReactServerErrorHandler(
}

export function createHTMLReactServerErrorHandler(
dev: boolean,
shouldFormatError: boolean,
isNextExport: boolean,
reactServerErrors: Map<string, DigestedError>,
silenceLogger: boolean,
Expand Down Expand Up @@ -120,7 +120,7 @@ export function createHTMLReactServerErrorHandler(
}

// Format server errors in development to add more helpful error messages
if (dev) {
if (shouldFormatError) {
formatServerError(err)
}

Expand Down Expand Up @@ -153,7 +153,7 @@ export function createHTMLReactServerErrorHandler(
}

export function createHTMLErrorHandler(
dev: boolean,
shouldFormatError: boolean,
isNextExport: boolean,
reactServerErrors: Map<string, DigestedError>,
allCapturedErrors: Array<unknown>,
Expand Down Expand Up @@ -200,7 +200,7 @@ export function createHTMLErrorHandler(
}

// Format server errors in development to add more helpful error messages
if (dev) {
if (shouldFormatError) {
formatServerError(err)
}

Expand Down
13 changes: 10 additions & 3 deletions packages/next/src/server/use-cache/use-cache-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type { CacheHandler, CacheEntry } from '../lib/cache-handlers/types'
import type { CacheSignal } from '../app-render/cache-signal'
import { decryptActionBoundArgs } from '../app-render/encryption'
import { InvariantError } from '../../shared/lib/invariant-error'
import { createFlightReactServerErrorHandler } from '../app-render/create-error-handler'

const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'

Expand Down Expand Up @@ -330,11 +331,17 @@ async function generateCacheEntryImpl(
environmentName: 'Cache',
signal: controller.signal,
temporaryReferences,
onError(error: unknown) {
// Report the error.
// In the "Cache" environment, we only need to make sure that the error
// digests are handled correctly. Error formatting and reporting is not
// necessary here; the errors are encoded in the stream, and will be
// reported in the "Server" environment.
onError: createFlightReactServerErrorHandler(false, (error: unknown) => {
// TODO: For now we're also reporting the error here, because in
// production, the "Server" environment will only get the obfuscated
// error (created by the Flight Client in the cache wrapper).
console.error(error)
errors.push(error)
},
}),
}
)

Expand Down
9 changes: 9 additions & 0 deletions test/e2e/app-dir/use-cache/app/not-found/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use cache'

import { notFound } from 'next/navigation'

export default async function Page() {
notFound()

return <p>This will never render</p>
}
6 changes: 6 additions & 0 deletions test/e2e/app-dir/use-cache/use-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,10 @@ describe('use-cache', () => {
expect(await browser.elementByCss('p').text()).toBe(value)
})
})

it('renders the not-found page when `notFound()` is used', async () => {
const browser = await next.browser('/not-found')
const text = await browser.elementByCss('h2').text()
expect(text).toBe('This page could not be found.')
})
})

0 comments on commit c37b182

Please sign in to comment.