Unverified Commit 223c9747 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Fix router.refresh() resetting state and Suspense boundaries (#44775)

Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent e0aac6f4
...@@ -157,7 +157,6 @@ export function InnerLayoutRouter({ ...@@ -157,7 +157,6 @@ export function InnerLayoutRouter({
// TODO-APP: implement `<Offscreen>` when available. // TODO-APP: implement `<Offscreen>` when available.
// isActive, // isActive,
path, path,
rootLayoutIncluded,
}: { }: {
parallelRouterKey: string parallelRouterKey: string
url: string url: string
...@@ -167,7 +166,6 @@ export function InnerLayoutRouter({ ...@@ -167,7 +166,6 @@ export function InnerLayoutRouter({
tree: FlightRouterState tree: FlightRouterState
isActive: boolean isActive: boolean
path: string path: string
rootLayoutIncluded: boolean
}) { }) {
const context = useContext(GlobalLayoutRouterContext) const context = useContext(GlobalLayoutRouterContext)
if (!context) { if (!context) {
...@@ -295,12 +293,10 @@ export function InnerLayoutRouter({ ...@@ -295,12 +293,10 @@ export function InnerLayoutRouter({
) )
// Ensure root layout is not wrapped in a div as the root layout renders `<html>` // Ensure root layout is not wrapped in a div as the root layout renders `<html>`
return rootLayoutIncluded ? ( return (
<ScrollAndFocusHandler focusAndScrollRef={focusAndScrollRef}> <ScrollAndFocusHandler focusAndScrollRef={focusAndScrollRef}>
{subtree} {subtree}
</ScrollAndFocusHandler> </ScrollAndFocusHandler>
) : (
subtree
) )
} }
...@@ -455,7 +451,6 @@ export default function OuterLayoutRouter({ ...@@ -455,7 +451,6 @@ export default function OuterLayoutRouter({
template, template,
notFound, notFound,
notFoundStyles, notFoundStyles,
rootLayoutIncluded,
}: { }: {
parallelRouterKey: string parallelRouterKey: string
segmentPath: FlightSegmentPath segmentPath: FlightSegmentPath
...@@ -469,7 +464,6 @@ export default function OuterLayoutRouter({ ...@@ -469,7 +464,6 @@ export default function OuterLayoutRouter({
hasLoading: boolean hasLoading: boolean
notFound: React.ReactNode | undefined notFound: React.ReactNode | undefined
notFoundStyles: React.ReactNode | undefined notFoundStyles: React.ReactNode | undefined
rootLayoutIncluded: boolean
}) { }) {
const context = useContext(LayoutRouterContext) const context = useContext(LayoutRouterContext)
if (!context) { if (!context) {
...@@ -546,7 +540,6 @@ export default function OuterLayoutRouter({ ...@@ -546,7 +540,6 @@ export default function OuterLayoutRouter({
segmentPath={segmentPath} segmentPath={segmentPath}
path={preservedSegment} path={preservedSegment}
isActive={currentChildSegment === preservedSegment} isActive={currentChildSegment === preservedSegment}
rootLayoutIncluded={rootLayoutIncluded}
/> />
</RedirectBoundary> </RedirectBoundary>
</NotFoundBoundary> </NotFoundBoundary>
......
...@@ -834,9 +834,11 @@ function clientReducer( ...@@ -834,9 +834,11 @@ function clientReducer(
// Handles case where prefetch only returns the router tree patch without rendered components. // Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData !== null) { if (subTreeData !== null) {
if (flightDataPath.length === 3) { if (flightDataPath.length === 3) {
cache.status = CacheStates.READY
cache.subTreeData = subTreeData cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head) fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head)
} else { } else {
cache.status = CacheStates.READY
// Copy subTreeData for the root node of the cache. // Copy subTreeData for the root node of the cache.
cache.subTreeData = state.cache.subTreeData cache.subTreeData = state.cache.subTreeData
// Create a copy of the existing cache with the subTreeData applied. // Create a copy of the existing cache with the subTreeData applied.
...@@ -855,6 +857,7 @@ function clientReducer( ...@@ -855,6 +857,7 @@ function clientReducer(
) )
if (hardNavigate) { if (hardNavigate) {
cache.status = CacheStates.READY
// Copy subTreeData for the root node of the cache. // Copy subTreeData for the root node of the cache.
cache.subTreeData = state.cache.subTreeData cache.subTreeData = state.cache.subTreeData
...@@ -915,6 +918,7 @@ function clientReducer( ...@@ -915,6 +918,7 @@ function clientReducer(
) )
// Copy subTreeData for the root node of the cache. // Copy subTreeData for the root node of the cache.
cache.status = CacheStates.READY
cache.subTreeData = state.cache.subTreeData cache.subTreeData = state.cache.subTreeData
// Copy existing cache nodes as far as possible and fill in `data` property with the started data fetch. // Copy existing cache nodes as far as possible and fill in `data` property with the started data fetch.
...@@ -1012,10 +1016,12 @@ function clientReducer( ...@@ -1012,10 +1016,12 @@ function clientReducer(
mutable.mpaNavigation = isNavigatingToNewRootLayout(state.tree, newTree) mutable.mpaNavigation = isNavigatingToNewRootLayout(state.tree, newTree)
if (flightDataPath.length === 3) { if (flightDataPath.length === 3) {
cache.status = CacheStates.READY
cache.subTreeData = subTreeData cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head) fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head)
} else { } else {
// Copy subTreeData for the root node of the cache. // Copy subTreeData for the root node of the cache.
cache.status = CacheStates.READY
cache.subTreeData = state.cache.subTreeData cache.subTreeData = state.cache.subTreeData
// Create a copy of the existing cache with the subTreeData applied. // Create a copy of the existing cache with the subTreeData applied.
fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath) fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath)
...@@ -1137,10 +1143,12 @@ function clientReducer( ...@@ -1137,10 +1143,12 @@ function clientReducer(
// Root refresh // Root refresh
if (flightDataPath.length === 3) { if (flightDataPath.length === 3) {
cache.status = CacheStates.READY
cache.subTreeData = subTreeData cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head) fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head)
} else { } else {
// Copy subTreeData for the root node of the cache. // Copy subTreeData for the root node of the cache.
cache.status = CacheStates.READY
cache.subTreeData = state.cache.subTreeData cache.subTreeData = state.cache.subTreeData
fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath) fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath)
} }
...@@ -1288,6 +1296,7 @@ function clientReducer( ...@@ -1288,6 +1296,7 @@ function clientReducer(
mutable.mpaNavigation = isNavigatingToNewRootLayout(state.tree, newTree) mutable.mpaNavigation = isNavigatingToNewRootLayout(state.tree, newTree)
// Set subTreeData for the root node of the cache. // Set subTreeData for the root node of the cache.
cache.status = CacheStates.READY
cache.subTreeData = subTreeData cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head) fillLazyItemsTillLeafWithHead(cache, state.cache, treePatch, head)
......
...@@ -1342,7 +1342,6 @@ export async function renderToHTMLOrFlight( ...@@ -1342,7 +1342,6 @@ export async function renderToHTMLOrFlight(
notFound={NotFound ? <NotFound /> : undefined} notFound={NotFound ? <NotFound /> : undefined}
notFoundStyles={notFoundStyles} notFoundStyles={notFoundStyles}
childProp={childProp} childProp={childProp}
rootLayoutIncluded={rootLayoutIncludedAtThisLevelOrAbove}
/>, />,
] ]
} }
...@@ -1387,7 +1386,6 @@ export async function renderToHTMLOrFlight( ...@@ -1387,7 +1386,6 @@ export async function renderToHTMLOrFlight(
notFound={NotFound ? <NotFound /> : undefined} notFound={NotFound ? <NotFound /> : undefined}
notFoundStyles={notFoundStyles} notFoundStyles={notFoundStyles}
childProp={childProp} childProp={childProp}
rootLayoutIncluded={rootLayoutIncludedAtThisLevelOrAbove}
/>, />,
] ]
} }
......
'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>
)}
</>
)
}
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>
</>
)
}
...@@ -2285,6 +2285,30 @@ createNextDescribe( ...@@ -2285,6 +2285,30 @@ createNextDescribe(
expect($('#category-id').text()).toBe('electronicsabc') 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 () => { it('should handle as on next/link', async () => {
const browser = await next.browser('/link-with-as') const browser = await next.browser('/link-with-as')
expect( expect(
......
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