Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
8b802ddb
Unverified
Commit
8b802ddb
authored
Sep 26, 2022
by
Tim Neutkens
Committed by
GitHub
Sep 26, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update handling of redirect/404 throw to cross server->client boundary (#40890)
parent
3211b3f6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
16 additions
and
31 deletions
+16
-31
layout-router.client.tsx
packages/next/client/components/layout-router.client.tsx
+4
-3
not-found.ts
packages/next/client/components/not-found.ts
+3
-3
redirect.ts
packages/next/client/components/redirect.ts
+1
-2
worker.ts
packages/next/export/worker.ts
+3
-1
app-render.tsx
packages/next/server/app-render.tsx
+4
-20
index.test.ts
test/e2e/app-dir/index.test.ts
+1
-2
No files found.
packages/next/client/components/layout-router.client.tsx
View file @
8b802ddb
...
@@ -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
...
...
packages/next/client/components/not-found.ts
View file @
8b802ddb
...
@@ -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
}
}
packages/next/client/components/redirect.ts
View file @
8b802ddb
...
@@ -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
}
}
packages/next/export/worker.ts
View file @
8b802ddb
...
@@ -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
}
}
...
...
packages/next/server/app-render.tsx
View file @
8b802ddb
...
@@ -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
=
{
...
...
test/e2e/app-dir/index.test.ts
View file @
8b802ddb
...
@@ -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'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment