Skip to content

Commit

Permalink
fix refreshing inactive segments that contained searchParams (#64086)
Browse files Browse the repository at this point in the history
When adding refresh markers for refetching segments that are "stale"
(the soft navigation case where they are still in the Router State Tree
but not part of the page that we're rendering), they only contained a
reference to the pathname. Once the actual refresh was triggered we
added the current search params to the request.

However, this causes an issue: segments in the `FlightRouterState` can
contain searchParams (ie, `__PAGE__?{"foo": "bar}` is what the segment
becomes when adding `?foo=bar` to a page). This means that when we
refresh those nodes from the server, it won't know how to find the
`CacheNode` for the stale segment since we won't have them in the
refetch tree. This updates to keep a reference to the searchParams that
were part of the request that made it active.

While fixing this, I also noticed that we were missing a spot to add the
refetch marker in `applyRouterStatePatchToTree` in the case of a root
refresh (caught by these tests failing in PPR)


[x-ref](#63900 (reply in thread))

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-3007
  • Loading branch information
ztanner authored Apr 4, 2024
1 parent 3d3311b commit 51549d9
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export function applyRouterStatePatchToTree(
flightSegmentPath
)

addRefreshMarkerToActiveParallelSegments(tree, pathname)

return tree
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function fastRefreshReducerImpl(
[''],
currentTree,
treePatch,
location.pathname
state.canonicalUrl
)

if (newTree === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function navigateReducer_noPPR(
flightSegmentPathWithLeadingEmpty,
currentTree,
treePatch,
url.pathname
href
)

// If the tree patch can't be applied to the current tree then we use the tree at time of prefetch
Expand All @@ -187,7 +187,7 @@ function navigateReducer_noPPR(
flightSegmentPathWithLeadingEmpty,
treeAtTimeOfPrefetch,
treePatch,
url.pathname
href
)
}

Expand Down Expand Up @@ -354,7 +354,7 @@ function navigateReducer_PPR(
flightSegmentPathWithLeadingEmpty,
currentTree,
treePatch,
url.pathname
href
)

// If the tree patch can't be applied to the current tree then we use the tree at time of prefetch
Expand All @@ -365,7 +365,7 @@ function navigateReducer_PPR(
flightSegmentPathWithLeadingEmpty,
treeAtTimeOfPrefetch,
treePatch,
url.pathname
href
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function refreshReducer(
[''],
currentTree,
treePatch,
location.pathname
state.canonicalUrl
)

if (newTree === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export function serverActionReducer(
[''],
currentTree,
treePatch,
location.pathname
href
)

if (newTree === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function serverPatchReducer(
['', ...flightSegmentPath],
currentTree,
treePatch,
location.pathname
state.canonicalUrl
)

if (newTree === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ async function refreshInactiveParallelSegmentsImpl({
// Eagerly kick off the fetch for the refetch path & the parallel routes. This should be fine to do as they each operate
// independently on their own cache nodes, and `applyFlightData` will copy anything it doesn't care about from the existing cache.
const fetchPromise = fetchServerResponse(
// we capture the pathname of the refetch without search params, so that it can be refetched with
// the "latest" search params when it comes time to actually trigger the fetch (below)
new URL(refetchPathname + location.search, location.origin),
new URL(refetchPathname, location.origin),
// refetch from the root of the updated tree, otherwise it will be scoped to the current segment
// and might not contain the data we need to patch in interception route data (such as dynamic params from a previous segment)
[rootTree[0], rootTree[1], rootTree[2], 'refetch'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client'
import { useRouter } from 'next/navigation'

export function UpdateSearchParamsButton({
searchParams,
id,
}: {
searchParams: any
id?: string
}) {
const router = useRouter()

return (
<div>
<div id={`search-params${id ? `-${id}` : ''}`}>
Params: {JSON.stringify(searchParams.random)}
</div>
<button
id={`update-search-params${id ? `-${id}` : ''}`}
style={{ color: 'blue', padding: '10px' }}
onClick={() => router.replace(`?random=${Math.random()}#hash-test`)}
>
Add Search Params
</button>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { RefreshButton } from '../../../../components/RefreshButton'
import { RevalidateButton } from '../../../../components/RevalidateButton'
import { UpdateSearchParamsButton } from '../../../../components/UpdateSearchParamsButton'

const getRandom = async () => Math.random()

export default async function Page({ params }) {
export default async function Page({ params, searchParams }) {
const someProp = await getRandom()

return (
Expand All @@ -16,6 +17,7 @@ export default async function Page({ params }) {
</div>
<RefreshButton />
<RevalidateButton />
<UpdateSearchParamsButton searchParams={searchParams} id="modal" />
</div>
</dialog>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Link from 'next/link'
import { UpdateSearchParamsButton } from '../../components/UpdateSearchParamsButton'

export default function Home() {
export default function Home({ searchParams }) {
return (
<main>
<Link href="/dynamic-refresh/foo/login">
Expand All @@ -9,6 +10,7 @@ export default function Home() {
<div>
Random # from Root Page: <span id="random-number">{Math.random()}</span>
</div>
<UpdateSearchParamsButton searchParams={searchParams} />
</main>
)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { RefreshButton } from '../../../components/RefreshButton'
import { RevalidateButton } from '../../../components/RevalidateButton'
import { UpdateSearchParamsButton } from '../../../components/UpdateSearchParamsButton'

const getRandom = async () => Math.random()

export default async function Page() {
export default async function Page({ searchParams }) {
const someProp = await getRandom()

return (
Expand All @@ -15,6 +16,7 @@ export default async function Page() {
</div>
<RefreshButton />
<RevalidateButton />
<UpdateSearchParamsButton searchParams={searchParams} id="modal" />
</div>
</dialog>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Link from 'next/link'
import { UpdateSearchParamsButton } from '../components/UpdateSearchParamsButton'

export default function Home() {
export default function Home({ searchParams }) {
return (
<main>
<Link href="/refreshing/login">
Expand All @@ -9,6 +10,7 @@ export default function Home() {
<div>
Random # from Root Page: <span id="random-number">{Math.random()}</span>
</div>
<UpdateSearchParamsButton searchParams={searchParams} />
</main>
)
}
Loading

0 comments on commit 51549d9

Please sign in to comment.