Commit f7ec039f authored by Zack Tanner's avatar Zack Tanner

prevent duplicate RSC fetch when action redirects (#66620)

When checking which segment(s) need to be refreshed, we currently
compare the current page URL with the segment's refresh marker.

We should inspect the `mutable.canonicalUrl` value first since that's
the URL we're changing to, followed by `state.canonicalUrl` as a
fallback (indicating that there's no URL change pending). This is
because the server action handler will receive a redirect URL prior to
`location.pathname` being updated, so the router will incorrectly think
it needs to refresh the data for the page we're going to.

Closes NEXT-3500
parent dd6ab93d
...@@ -122,6 +122,7 @@ export function refreshReducer( ...@@ -122,6 +122,7 @@ export function refreshReducer(
updatedTree: newTree, updatedTree: newTree,
updatedCache: cache, updatedCache: cache,
includeNextUrl, includeNextUrl,
canonicalUrl: mutable.canonicalUrl || state.canonicalUrl,
}) })
mutable.cache = cache mutable.cache = cache
......
...@@ -210,6 +210,11 @@ export function serverActionReducer( ...@@ -210,6 +210,11 @@ export function serverActionReducer(
// Remove cache.data as it has been resolved at this point. // Remove cache.data as it has been resolved at this point.
mutable.inFlightServerAction = null mutable.inFlightServerAction = null
if (redirectLocation) {
const newHref = createHrefFromUrl(redirectLocation, false)
mutable.canonicalUrl = newHref
}
for (const flightDataPath of flightData) { for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned // FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) { if (flightDataPath.length !== 3) {
...@@ -266,6 +271,7 @@ export function serverActionReducer( ...@@ -266,6 +271,7 @@ export function serverActionReducer(
updatedTree: newTree, updatedTree: newTree,
updatedCache: cache, updatedCache: cache,
includeNextUrl: Boolean(nextUrl), includeNextUrl: Boolean(nextUrl),
canonicalUrl: mutable.canonicalUrl || state.canonicalUrl,
}) })
mutable.cache = cache mutable.cache = cache
...@@ -273,16 +279,9 @@ export function serverActionReducer( ...@@ -273,16 +279,9 @@ export function serverActionReducer(
} }
mutable.patchedTree = newTree mutable.patchedTree = newTree
mutable.canonicalUrl = href
currentTree = newTree currentTree = newTree
} }
if (redirectLocation) {
const newHref = createHrefFromUrl(redirectLocation, false)
mutable.canonicalUrl = newHref
}
resolve(actionResult) resolve(actionResult)
return handleMutable(state, mutable) return handleMutable(state, mutable)
......
...@@ -10,6 +10,7 @@ interface RefreshInactiveParallelSegments { ...@@ -10,6 +10,7 @@ interface RefreshInactiveParallelSegments {
updatedTree: FlightRouterState updatedTree: FlightRouterState
updatedCache: CacheNode updatedCache: CacheNode
includeNextUrl: boolean includeNextUrl: boolean
canonicalUrl: string
} }
/** /**
...@@ -41,6 +42,7 @@ async function refreshInactiveParallelSegmentsImpl({ ...@@ -41,6 +42,7 @@ async function refreshInactiveParallelSegmentsImpl({
includeNextUrl, includeNextUrl,
fetchedSegments, fetchedSegments,
rootTree = updatedTree, rootTree = updatedTree,
canonicalUrl,
}: RefreshInactiveParallelSegments & { }: RefreshInactiveParallelSegments & {
fetchedSegments: Set<string> fetchedSegments: Set<string>
rootTree: FlightRouterState rootTree: FlightRouterState
...@@ -50,7 +52,7 @@ async function refreshInactiveParallelSegmentsImpl({ ...@@ -50,7 +52,7 @@ async function refreshInactiveParallelSegmentsImpl({
if ( if (
refetchPath && refetchPath &&
refetchPath !== location.pathname + location.search && refetchPath !== canonicalUrl &&
refetchMarker === 'refresh' && refetchMarker === 'refresh' &&
// it's possible for the tree to contain multiple segments that contain data at the same URL // it's possible for the tree to contain multiple segments that contain data at the same URL
// we keep track of them so we can dedupe the requests // we keep track of them so we can dedupe the requests
...@@ -94,6 +96,7 @@ async function refreshInactiveParallelSegmentsImpl({ ...@@ -94,6 +96,7 @@ async function refreshInactiveParallelSegmentsImpl({
includeNextUrl, includeNextUrl,
fetchedSegments, fetchedSegments,
rootTree, rootTree,
canonicalUrl,
}) })
fetchPromises.push(parallelFetchPromise) fetchPromises.push(parallelFetchPromise)
......
...@@ -8,7 +8,7 @@ export default async function Home() { ...@@ -8,7 +8,7 @@ export default async function Home() {
).then((res) => res.text()) ).then((res) => res.text())
return ( return (
<div> <div id="root-page">
<Link href="/revalidate-modal">Open Revalidate Modal</Link> <Link href="/revalidate-modal">Open Revalidate Modal</Link>
<Link href="/refresh-modal">Open Refresh Modal</Link> <Link href="/refresh-modal">Open Refresh Modal</Link>
<Link href="/redirect-modal">Open Redirect Modal</Link> <Link href="/redirect-modal">Open Redirect Modal</Link>
......
...@@ -8,7 +8,9 @@ export default function Page() { ...@@ -8,7 +8,9 @@ export default function Page() {
redirect('/') redirect('/')
}} }}
> >
<button type="submit">Redirect</button> <button type="submit" id="redirect-page">
Redirect
</button>
</form> </form>
) )
} }
...@@ -6,7 +6,7 @@ createNextDescribe( ...@@ -6,7 +6,7 @@ createNextDescribe(
{ {
files: __dirname, files: __dirname,
}, },
({ next }) => { ({ next, isNextStart }) => {
it('should submit the action and revalidate the page data', async () => { it('should submit the action and revalidate the page data', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
await check(() => browser.hasElementByCssSelector('#create-entry'), false) await check(() => browser.hasElementByCssSelector('#create-entry'), false)
...@@ -423,5 +423,38 @@ createNextDescribe( ...@@ -423,5 +423,38 @@ createNextDescribe(
}) })
}) })
}) })
it('should not trigger a refresh for the page that is being redirected to', async () => {
const rscRequests = []
const prefetchRequests = []
const browser = await next.browser('/redirect', {
beforePageLoad(page) {
page.on('request', async (req) => {
const headers = await req.allHeaders()
if (headers['rsc']) {
const pathname = new URL(req.url()).pathname
if (headers['next-router-prefetch']) {
prefetchRequests.push(pathname)
} else {
rscRequests.push(pathname)
}
}
})
},
})
await browser.elementByCss('button').click()
await browser.waitForElementByCss('#root-page')
await browser.waitForIdleNetwork()
await retry(async () => {
expect(rscRequests.length).toBe(0)
if (isNextStart) {
expect(prefetchRequests.length).toBe(4)
}
})
})
} }
) )
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