Unverified Commit 8b802ddb authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Update handling of redirect/404 throw to cross server->client boundary (#40890)

parent 3211b3f6
...@@ -309,8 +309,9 @@ class RedirectErrorBoundary extends React.Component< ...@@ -309,8 +309,9 @@ class RedirectErrorBoundary extends React.Component<
} }
static getDerivedStateFromError(error: any) { static getDerivedStateFromError(error: any) {
if (error.digest === 'NEXT_REDIRECT') { if (error.digest?.startsWith('NEXT_REDIRECT')) {
return { redirect: error.url } const url = error.digest.split(';')[1]
return { redirect: url }
} }
// Re-throw if error is not for 404 // Re-throw if error is not for 404
throw error throw error
...@@ -354,7 +355,7 @@ class NotFoundErrorBoundary extends React.Component< ...@@ -354,7 +355,7 @@ class NotFoundErrorBoundary extends React.Component<
} }
static getDerivedStateFromError(error: any) { static getDerivedStateFromError(error: any) {
if (error.code === 'NEXT_NOT_FOUND') { if (error.digest === 'NEXT_NOT_FOUND') {
return { notFoundTriggered: true } return { notFoundTriggered: true }
} }
// Re-throw if error is not for 404 // Re-throw if error is not for 404
......
...@@ -2,7 +2,7 @@ export const NOT_FOUND_ERROR_CODE = 'NEXT_NOT_FOUND' ...@@ -2,7 +2,7 @@ export const NOT_FOUND_ERROR_CODE = 'NEXT_NOT_FOUND'
export function notFound() { export function notFound() {
// eslint-disable-next-line no-throw-literal // eslint-disable-next-line no-throw-literal
throw { const error = new Error(NOT_FOUND_ERROR_CODE)
code: NOT_FOUND_ERROR_CODE, ;(error as any).digest = NOT_FOUND_ERROR_CODE
} throw error
} }
...@@ -3,7 +3,6 @@ export const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT' ...@@ -3,7 +3,6 @@ export const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT'
export function redirect(url: string) { export function redirect(url: string) {
// eslint-disable-next-line no-throw-literal // eslint-disable-next-line no-throw-literal
const error = new Error(REDIRECT_ERROR_CODE) const error = new Error(REDIRECT_ERROR_CODE)
;(error as any).url = url ;(error as any).digest = REDIRECT_ERROR_CODE + ';' + url
;(error as any).digest = REDIRECT_ERROR_CODE
throw error throw error
} }
...@@ -30,6 +30,7 @@ import { addRequestMeta } from '../server/request-meta' ...@@ -30,6 +30,7 @@ import { addRequestMeta } from '../server/request-meta'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { REDIRECT_ERROR_CODE } from '../client/components/redirect' import { REDIRECT_ERROR_CODE } from '../client/components/redirect'
import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context' import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context'
import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found'
loadRequireHook() loadRequireHook()
const envConfig = require('../shared/lib/runtime-config') const envConfig = require('../shared/lib/runtime-config')
...@@ -419,7 +420,8 @@ export default async function exportPage({ ...@@ -419,7 +420,8 @@ export default async function exportPage({
} catch (err: any) { } catch (err: any) {
if ( if (
err.digest !== DYNAMIC_ERROR_CODE && err.digest !== DYNAMIC_ERROR_CODE &&
err.digest !== REDIRECT_ERROR_CODE err.digest !== NOT_FOUND_ERROR_CODE &&
!err.digest?.startsWith(REDIRECT_ERROR_CODE)
) { ) {
throw err throw err
} }
......
...@@ -31,6 +31,7 @@ import type { ComponentsType } from '../build/webpack/loaders/next-app-loader' ...@@ -31,6 +31,7 @@ import type { ComponentsType } from '../build/webpack/loaders/next-app-loader'
import { REDIRECT_ERROR_CODE } from '../client/components/redirect' import { REDIRECT_ERROR_CODE } from '../client/components/redirect'
import { NextCookies } from './web/spec-extension/cookies' import { NextCookies } from './web/spec-extension/cookies'
import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context' import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context'
import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found'
const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly') const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly')
...@@ -173,7 +174,8 @@ function createErrorHandler( ...@@ -173,7 +174,8 @@ function createErrorHandler(
if ( if (
// TODO-APP: Handle redirect throw // TODO-APP: Handle redirect throw
err.digest !== DYNAMIC_ERROR_CODE && err.digest !== DYNAMIC_ERROR_CODE &&
err.digest !== REDIRECT_ERROR_CODE err.digest !== NOT_FOUND_ERROR_CODE &&
!err.digest?.startsWith(REDIRECT_ERROR_CODE)
) { ) {
// Used for debugging error source // Used for debugging error source
// console.error(_source, err) // console.error(_source, err)
...@@ -1299,10 +1301,6 @@ export async function renderToHTMLOrFlight( ...@@ -1299,10 +1301,6 @@ export async function renderToHTMLOrFlight(
flushEffectsToHead: true, flushEffectsToHead: true,
}) })
} catch (err: any) { } catch (err: any) {
if (err.digest === REDIRECT_ERROR_CODE) {
throw err
}
// TODO-APP: show error overlay in development. `element` should probably be wrapped in AppRouter for this case. // TODO-APP: show error overlay in development. `element` should probably be wrapped in AppRouter for this case.
const renderStream = await renderToInitialStream({ const renderStream = await renderToInitialStream({
ReactDOMServer, ReactDOMServer,
...@@ -1358,21 +1356,7 @@ export async function renderToHTMLOrFlight( ...@@ -1358,21 +1356,7 @@ export async function renderToHTMLOrFlight(
return new RenderResult(staticHtml) return new RenderResult(staticHtml)
} }
try { return new RenderResult(await bodyResult())
return new RenderResult(await bodyResult())
} catch (err: any) {
if (err.digest === REDIRECT_ERROR_CODE) {
;(renderOpts as any).pageData = {
pageProps: {
__N_REDIRECT: err.url,
__N_REDIRECT_STATUS: 307,
},
}
;(renderOpts as any).isRedirect = true
return RenderResult.fromStatic('')
}
throw err
}
} }
const initialStaticGenerationStore = { const initialStaticGenerationStore = {
......
...@@ -1490,8 +1490,7 @@ describe('app dir', () => { ...@@ -1490,8 +1490,7 @@ describe('app dir', () => {
await browser.waitForElementByCss('#not-found-component').text() await browser.waitForElementByCss('#not-found-component').text()
).toBe('404!') ).toBe('404!')
}) })
;(isDev ? it.skip : it)('should trigger 404 client-side', async () => {
it('should trigger 404 client-side', async () => {
const browser = await webdriver(next.url, '/not-found/client-side') const browser = await webdriver(next.url, '/not-found/client-side')
await browser await browser
.elementByCss('button') .elementByCss('button')
......
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