Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make notFound() work in "use cache" page #73210

Merged
merged 1 commit into from
Nov 29, 2024
Merged
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
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.')
})
})
Loading