Unverified Commit 25ba8a74 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

app router: don't unmount/remount on search param changes (#49047)

parent 905cb5a5
......@@ -564,7 +564,7 @@ export default function OuterLayoutRouter({
- Passed to the router during rendering to ensure it can be immediately rendered when suspending on a Flight fetch.
*/
<TemplateContext.Provider
key={cacheKey}
key={createRouterCacheKey(preservedSegment, true)}
value={
<ScrollAndFocusHandler segmentPath={segmentPath}>
<ErrorBoundary errorComponent={error} errorStyles={errorStyles}>
......
import { Segment } from '../../../server/app-render/types'
export function createRouterCacheKey(segment: Segment) {
export function createRouterCacheKey(
segment: Segment,
withoutSearchParameters: boolean = false
) {
return Array.isArray(segment)
? `${segment[0]}|${segment[1]}|${segment[2]}`
: withoutSearchParameters && segment.startsWith('__PAGE__')
? '__PAGE__'
: segment
}
'use client'
import { useRouter } from 'next/navigation'
import React, { useState } from 'react'
export default () => {
const router = useRouter()
const [count, setCount] = useState(0)
return (
<>
<div id="count">{count}</div>
<button id="increment" onClick={() => setCount(count + 1)}>
increment
</button>
<button id="push" onClick={() => router.push('/?foo=bar')}>
push to /?foo=bar
</button>
<button id="replace" onClick={() => router.replace('/?foo=baz')}>
replace with /?foo=baz
</button>
</>
)
}
import React from 'react'
export default function Layout({
children,
}: {
children: React.ReactNode
params: {}
}) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
import React from 'react'
import ClientComponent from './client-component'
export default ({ searchParams }) => {
return (
<div>
<div id="search-params">{JSON.stringify(searchParams)}</div>
<ClientComponent />
</div>
)
}
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
createNextDescribe(
'app dir - search params keys',
{
files: __dirname,
},
({ next }) => {
it('should keep the React router instance the same when changing the search params', async () => {
const browser = await next.browser('/')
const searchParams = browser.waitForElementByCss('#search-params').text()
await browser.elementByCss('#increment').click()
await browser.elementByCss('#increment').click()
await browser.elementByCss('#push').click()
await check(async () => {
const newSearchParams = await browser
.waitForElementByCss('#search-params')
.text()
const count = await browser.waitForElementByCss('#count').text()
return newSearchParams !== searchParams && count === '2'
? 'success'
: 'retry'
}, 'success')
await browser.elementByCss('#increment').click()
await browser.elementByCss('#increment').click()
await browser.elementByCss('#replace').click()
await check(async () => {
const newSearchParams = await browser
.waitForElementByCss('#search-params')
.text()
const count = await browser.waitForElementByCss('#count').text()
return newSearchParams !== searchParams && count === '4'
? 'success'
: 'retry'
}, 'success')
})
}
)
module.exports = {
experimental: {
appDir: true,
},
}
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