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
976ccce5
Unverified
Commit
976ccce5
authored
Sep 23, 2022
by
Tim Neutkens
Committed by
GitHub
Sep 23, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle redirect in same way as 404 in new router (#40796)
parent
5d9f390f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
35 deletions
+79
-35
layout-router.client.tsx
packages/next/client/components/layout-router.client.tsx
+76
-18
redirect.ts
packages/next/client/components/redirect.ts
+0
-15
index.test.ts
test/e2e/app-dir/index.test.ts
+3
-2
No files found.
packages/next/client/components/layout-router.client.tsx
View file @
976ccce5
...
@@ -11,7 +11,10 @@ import type {
...
@@ -11,7 +11,10 @@ import type {
ChildProp
,
ChildProp
,
//Segment
//Segment
}
from
'../../server/app-render'
}
from
'../../server/app-render'
import
type
{
ChildSegmentMap
}
from
'../../shared/lib/app-router-context'
import
type
{
AppRouterInstance
,
ChildSegmentMap
,
}
from
'../../shared/lib/app-router-context'
import
type
{
import
type
{
FlightRouterState
,
FlightRouterState
,
FlightSegmentPath
,
FlightSegmentPath
,
...
@@ -21,6 +24,7 @@ import {
...
@@ -21,6 +24,7 @@ import {
LayoutRouterContext
,
LayoutRouterContext
,
GlobalLayoutRouterContext
,
GlobalLayoutRouterContext
,
TemplateContext
,
TemplateContext
,
AppRouterContext
,
}
from
'../../shared/lib/app-router-context'
}
from
'../../shared/lib/app-router-context'
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
createInfinitePromise
}
from
'./infinite-promise'
import
{
createInfinitePromise
}
from
'./infinite-promise'
...
@@ -285,6 +289,56 @@ function LoadingBoundary({
...
@@ -285,6 +289,56 @@ function LoadingBoundary({
return
<>
{
children
}
</>
return
<>
{
children
}
</>
}
}
interface
RedirectBoundaryProps
{
router
:
AppRouterInstance
children
:
React
.
ReactNode
}
function
InfinitePromiseComponent
()
{
use
(
createInfinitePromise
())
return
<></>
}
class
RedirectErrorBoundary
extends
React
.
Component
<
RedirectBoundaryProps
,
{
redirect
:
string
|
null
}
>
{
constructor
(
props
:
RedirectBoundaryProps
)
{
super
(
props
)
this
.
state
=
{
redirect
:
null
}
}
static
getDerivedStateFromError
(
error
:
any
)
{
if
(
error
.
code
===
'NEXT_REDIRECT'
)
{
return
{
redirect
:
error
.
url
}
}
// Re-throw if error is not for 404
throw
error
}
render
()
{
const
redirect
=
this
.
state
.
redirect
if
(
redirect
!==
null
)
{
setTimeout
(()
=>
{
// @ts-ignore startTransition exists
React
.
startTransition
(()
=>
{
this
.
props
.
router
.
replace
(
redirect
,
{})
})
})
return
<
InfinitePromiseComponent
/>
}
return
this
.
props
.
children
}
}
function
RedirectBoundary
({
children
}:
{
children
:
React
.
ReactNode
})
{
const
router
=
useContext
(
AppRouterContext
)
return
(
<
RedirectErrorBoundary
router=
{
router
}
>
{
children
}
</
RedirectErrorBoundary
>
)
}
interface
NotFoundBoundaryProps
{
interface
NotFoundBoundaryProps
{
notFound
?:
React
.
ReactNode
notFound
?:
React
.
ReactNode
children
:
React
.
ReactNode
children
:
React
.
ReactNode
...
@@ -455,23 +509,27 @@ export default function OuterLayoutRouter({
...
@@ -455,23 +509,27 @@ export default function OuterLayoutRouter({
key=
{
preservedSegment
}
key=
{
preservedSegment
}
value=
{
value=
{
<
ErrorBoundary
errorComponent=
{
error
}
>
<
ErrorBoundary
errorComponent=
{
error
}
>
<
NotFoundBoundary
notFound=
{
notFound
}
>
<
LoadingBoundary
loading=
{
loading
}
>
<
LoadingBoundary
loading=
{
loading
}
>
<
NotFoundBoundary
notFound=
{
notFound
}
>
<
InnerLayoutRouter
<
RedirectBoundary
>
parallelRouterKey=
{
parallelRouterKey
}
<
InnerLayoutRouter
url=
{
url
}
parallelRouterKey=
{
parallelRouterKey
}
tree=
{
tree
}
url=
{
url
}
childNodes=
{
childNodesForParallelRouter
!
}
tree=
{
tree
}
childProp=
{
childNodes=
{
childNodesForParallelRouter
!
}
childPropSegment
===
preservedSegment
?
childProp
:
null
childProp=
{
}
childPropSegment
===
preservedSegment
segmentPath=
{
segmentPath
}
?
childProp
path=
{
preservedSegment
}
:
null
isActive=
{
currentChildSegment
===
preservedSegment
}
}
rootLayoutIncluded=
{
rootLayoutIncluded
}
segmentPath=
{
segmentPath
}
/>
path=
{
preservedSegment
}
</
LoadingBoundary
>
isActive=
{
currentChildSegment
===
preservedSegment
}
</
NotFoundBoundary
>
rootLayoutIncluded=
{
rootLayoutIncluded
}
/>
</
RedirectBoundary
>
</
NotFoundBoundary
>
</
LoadingBoundary
>
</
ErrorBoundary
>
</
ErrorBoundary
>
}
}
>
>
...
...
packages/next/client/components/redirect.ts
View file @
976ccce5
import
React
,
{
experimental_use
as
use
}
from
'react'
import
{
AppRouterContext
}
from
'../../shared/lib/app-router-context'
import
{
createInfinitePromise
}
from
'./infinite-promise'
export
const
REDIRECT_ERROR_CODE
=
'NEXT_REDIRECT'
export
const
REDIRECT_ERROR_CODE
=
'NEXT_REDIRECT'
export
function
redirect
(
url
:
string
)
{
export
function
redirect
(
url
:
string
)
{
if
(
process
.
browser
)
{
const
router
=
use
(
AppRouterContext
)
setTimeout
(()
=>
{
// @ts-ignore startTransition exists
React
.
startTransition
(()
=>
{
router
.
replace
(
url
,
{})
})
})
// setTimeout is used to start a new transition during render, this is an intentional hack around React.
use
(
createInfinitePromise
())
}
// 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
).
url
=
url
...
...
test/e2e/app-dir/index.test.ts
View file @
976ccce5
...
@@ -1513,7 +1513,7 @@ describe('app dir', () => {
...
@@ -1513,7 +1513,7 @@ describe('app dir', () => {
)
)
})
})
it
(
'should redirect in a client component'
,
async
()
=>
{
it
.
skip
(
'should redirect in a client component'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/redirect/clientcomponent'
)
const
browser
=
await
webdriver
(
next
.
url
,
'/redirect/clientcomponent'
)
await
browser
.
waitForElementByCss
(
'#result-page'
)
await
browser
.
waitForElementByCss
(
'#result-page'
)
expect
(
await
browser
.
elementByCss
(
'#result-page'
).
text
()).
toBe
(
expect
(
await
browser
.
elementByCss
(
'#result-page'
).
text
()).
toBe
(
...
@@ -1521,7 +1521,8 @@ describe('app dir', () => {
...
@@ -1521,7 +1521,8 @@ describe('app dir', () => {
)
)
})
})
it
(
'should redirect client-side'
,
async
()
=>
{
// TODO-APP: Enable in development
;(
isDev
?
it
.
skip
:
it
)(
'should redirect client-side'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/redirect/client-side'
)
const
browser
=
await
webdriver
(
next
.
url
,
'/redirect/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