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
223c9747
Unverified
Commit
223c9747
authored
Jan 12, 2023
by
Tim Neutkens
Committed by
GitHub
Jan 12, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix router.refresh() resetting state and Suspense boundaries (#44775)
Co-authored-by:
JJ Kasper
<
jj@jjsweb.site
>
parent
e0aac6f4
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
86 additions
and
10 deletions
+86
-10
layout-router.tsx
packages/next/src/client/components/layout-router.tsx
+1
-8
reducer.ts
packages/next/src/client/components/reducer.ts
+9
-0
app-render.tsx
packages/next/src/server/app-render.tsx
+0
-2
page.js
...refresh/navigate-then-refresh-bug/page-to-refresh/page.js
+38
-0
page.js
.../app/navigation/refresh/navigate-then-refresh-bug/page.js
+14
-0
index.test.ts
test/e2e/app-dir/app/index.test.ts
+24
-0
No files found.
packages/next/src/client/components/layout-router.tsx
View file @
223c9747
...
...
@@ -157,7 +157,6 @@ export function InnerLayoutRouter({
// TODO-APP: implement `<Offscreen>` when available.
// isActive,
path
,
rootLayoutIncluded
,
}:
{
parallelRouterKey
:
string
url
:
string
...
...
@@ -167,7 +166,6 @@ export function InnerLayoutRouter({
tree
:
FlightRouterState
isActive
:
boolean
path
:
string
rootLayoutIncluded
:
boolean
})
{
const
context
=
useContext
(
GlobalLayoutRouterContext
)
if
(
!
context
)
{
...
...
@@ -295,12 +293,10 @@ export function InnerLayoutRouter({
)
// Ensure root layout is not wrapped in a div as the root layout renders `<html>`
return
rootLayoutIncluded
?
(
return
(
<
ScrollAndFocusHandler
focusAndScrollRef=
{
focusAndScrollRef
}
>
{
subtree
}
</
ScrollAndFocusHandler
>
)
:
(
subtree
)
}
...
...
@@ -455,7 +451,6 @@ export default function OuterLayoutRouter({
template
,
notFound
,
notFoundStyles
,
rootLayoutIncluded
,
}:
{
parallelRouterKey
:
string
segmentPath
:
FlightSegmentPath
...
...
@@ -469,7 +464,6 @@ export default function OuterLayoutRouter({
hasLoading
:
boolean
notFound
:
React
.
ReactNode
|
undefined
notFoundStyles
:
React
.
ReactNode
|
undefined
rootLayoutIncluded
:
boolean
})
{
const
context
=
useContext
(
LayoutRouterContext
)
if
(
!
context
)
{
...
...
@@ -546,7 +540,6 @@ export default function OuterLayoutRouter({
segmentPath=
{
segmentPath
}
path=
{
preservedSegment
}
isActive=
{
currentChildSegment
===
preservedSegment
}
rootLayoutIncluded=
{
rootLayoutIncluded
}
/>
</
RedirectBoundary
>
</
NotFoundBoundary
>
...
...
packages/next/src/client/components/reducer.ts
View file @
223c9747
...
...
@@ -834,9 +834,11 @@ function clientReducer(
// Handles case where prefetch only returns the router tree patch without rendered components.
if
(
subTreeData
!==
null
)
{
if
(
flightDataPath
.
length
===
3
)
{
cache
.
status
=
CacheStates
.
READY
cache
.
subTreeData
=
subTreeData
fillLazyItemsTillLeafWithHead
(
cache
,
state
.
cache
,
treePatch
,
head
)
}
else
{
cache
.
status
=
CacheStates
.
READY
// Copy subTreeData for the root node of the cache.
cache
.
subTreeData
=
state
.
cache
.
subTreeData
// Create a copy of the existing cache with the subTreeData applied.
...
...
@@ -855,6 +857,7 @@ function clientReducer(
)
if
(
hardNavigate
)
{
cache
.
status
=
CacheStates
.
READY
// Copy subTreeData for the root node of the cache.
cache
.
subTreeData
=
state
.
cache
.
subTreeData
...
...
@@ -915,6 +918,7 @@ function clientReducer(
)
// Copy subTreeData for the root node of the cache.
cache
.
status
=
CacheStates
.
READY
cache
.
subTreeData
=
state
.
cache
.
subTreeData
// Copy existing cache nodes as far as possible and fill in `data` property with the started data fetch.
...
...
@@ -1012,10 +1016,12 @@ function clientReducer(
mutable
.
mpaNavigation
=
isNavigatingToNewRootLayout
(
state
.
tree
,
newTree
)
if
(
flightDataPath
.
length
===
3
)
{
cache
.
status
=
CacheStates
.
READY
cache
.
subTreeData
=
subTreeData
fillLazyItemsTillLeafWithHead
(
cache
,
state
.
cache
,
treePatch
,
head
)
}
else
{
// Copy subTreeData for the root node of the cache.
cache
.
status
=
CacheStates
.
READY
cache
.
subTreeData
=
state
.
cache
.
subTreeData
// Create a copy of the existing cache with the subTreeData applied.
fillCacheWithNewSubTreeData
(
cache
,
state
.
cache
,
flightDataPath
)
...
...
@@ -1137,10 +1143,12 @@ function clientReducer(
// Root refresh
if
(
flightDataPath
.
length
===
3
)
{
cache
.
status
=
CacheStates
.
READY
cache
.
subTreeData
=
subTreeData
fillLazyItemsTillLeafWithHead
(
cache
,
state
.
cache
,
treePatch
,
head
)
}
else
{
// Copy subTreeData for the root node of the cache.
cache
.
status
=
CacheStates
.
READY
cache
.
subTreeData
=
state
.
cache
.
subTreeData
fillCacheWithNewSubTreeData
(
cache
,
state
.
cache
,
flightDataPath
)
}
...
...
@@ -1288,6 +1296,7 @@ function clientReducer(
mutable
.
mpaNavigation
=
isNavigatingToNewRootLayout
(
state
.
tree
,
newTree
)
// Set subTreeData for the root node of the cache.
cache
.
status
=
CacheStates
.
READY
cache
.
subTreeData
=
subTreeData
fillLazyItemsTillLeafWithHead
(
cache
,
state
.
cache
,
treePatch
,
head
)
...
...
packages/next/src/server/app-render.tsx
View file @
223c9747
...
...
@@ -1342,7 +1342,6 @@ export async function renderToHTMLOrFlight(
notFound=
{
NotFound
?
<
NotFound
/>
:
undefined
}
notFoundStyles=
{
notFoundStyles
}
childProp=
{
childProp
}
rootLayoutIncluded=
{
rootLayoutIncludedAtThisLevelOrAbove
}
/>,
]
}
...
...
@@ -1387,7 +1386,6 @@ export async function renderToHTMLOrFlight(
notFound=
{
NotFound
?
<
NotFound
/>
:
undefined
}
notFoundStyles=
{
notFoundStyles
}
childProp=
{
childProp
}
rootLayoutIncluded=
{
rootLayoutIncludedAtThisLevelOrAbove
}
/>,
]
}
...
...
test/e2e/app-dir/app/app/navigation/refresh/navigate-then-refresh-bug/page-to-refresh/page.js
0 → 100644
View file @
223c9747
'use client'
import
{
useRouter
}
from
'next/navigation'
import
{
startTransition
}
from
'react'
import
{
useCallback
}
from
'react'
import
{
useState
}
from
'react'
export
default
function
Page
()
{
const
[
success
,
setSuccess
]
=
useState
(
false
)
const
[
finishedRefresh
,
setFinishedRefresh
]
=
useState
(
false
)
const
router
=
useRouter
()
const
handleClick
=
useCallback
(()
=>
{
setSuccess
(
true
)
startTransition
(()
=>
{
router
.
refresh
()
setFinishedRefresh
(
true
)
})
},
[
router
])
return
(
<>
<
button
id
=
"refresh-page"
onClick
=
{
handleClick
}
>
Trigger
refresh
and
setState
<
/button
>
<
h1
style
=
{{
backgroundColor
:
success
?
'#228B22'
:
'#ccc'
,
color
:
'white'
,
}}
>
The
current
page
<
/h1
>
{
finishedRefresh
&&
(
<
div
id
=
"refreshed"
>
Refreshed
page
successfully
!<
/div
>
)}
<
/
>
)
}
test/e2e/app-dir/app/app/navigation/refresh/navigate-then-refresh-bug/page.js
0 → 100644
View file @
223c9747
import
Link
from
'next/link'
export
default
function
Page
()
{
return
(
<>
<
Link
href
=
"/navigation/refresh/navigate-then-refresh-bug/page-to-refresh"
id
=
"to-route"
>
To
route
<
/Link
>
<
/
>
)
}
test/e2e/app-dir/app/index.test.ts
View file @
223c9747
...
...
@@ -2285,6 +2285,30 @@ createNextDescribe(
expect
(
$
(
'#category-id'
).
text
()).
toBe
(
'electronicsabc'
)
}
})
it
(
'should handle router.refresh without resetting state'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/navigation/refresh/navigate-then-refresh-bug'
)
await
browser
.
elementByCss
(
'#to-route'
)
// Navigate to the page
.
click
()
// Wait for new page to be loaded
.
waitForElementByCss
(
'#refresh-page'
)
// Click the refresh button to trigger a refresh
.
click
()
// Wait for element that is shown when refreshed and verify text
expect
(
await
browser
.
waitForElementByCss
(
'#refreshed'
).
text
()).
toBe
(
'Refreshed page successfully!'
)
expect
(
await
browser
.
eval
(
`window.getComputedStyle(document.querySelector('h1')).backgroundColor`
)
).
toBe
(
'rgb(34, 139, 34)'
)
})
it
(
'should handle as on next/link'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/link-with-as'
)
expect
(
...
...
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