Unverified Commit 8a11835f authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Strip _rsc query for navigation between app and page (#51195)

Fix a missing case from #50970

When navigating from app to page, the `_rsc` query should still be stripped 
parent a6a9d65c
...@@ -85,9 +85,9 @@ export async function fetchServerResponse( ...@@ -85,9 +85,9 @@ export async function fetchServerResponse(
credentials: 'same-origin', credentials: 'same-origin',
headers, headers,
}) })
const canonicalUrl = res.redirected
? urlToUrlWithoutFlightMarker(res.url) const responseUrl = urlToUrlWithoutFlightMarker(res.url)
: undefined const canonicalUrl = res.redirected ? responseUrl : undefined
const contentType = res.headers.get('content-type') || '' const contentType = res.headers.get('content-type') || ''
let isFlightResponse = contentType === RSC_CONTENT_TYPE_HEADER let isFlightResponse = contentType === RSC_CONTENT_TYPE_HEADER
...@@ -103,7 +103,7 @@ export async function fetchServerResponse( ...@@ -103,7 +103,7 @@ export async function fetchServerResponse(
// If fetch returns something different than flight response handle it like a mpa navigation // If fetch returns something different than flight response handle it like a mpa navigation
// If the fetch was not 200, we also handle it like a mpa navigation // If the fetch was not 200, we also handle it like a mpa navigation
if (!isFlightResponse || !res.ok) { if (!isFlightResponse || !res.ok) {
return [res.url, undefined] return [responseUrl.toString(), undefined]
} }
// Handle the `fetch` readable stream that can be unwrapped by `React.use`. // Handle the `fetch` readable stream that can be unwrapped by `React.use`.
......
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
INTERCEPTION_ROUTE_MARKERS, INTERCEPTION_ROUTE_MARKERS,
isInterceptionRouteAppPath, isInterceptionRouteAppPath,
} from '../../../../server/future/helpers/interception-routes' } from '../../../../server/future/helpers/interception-routes'
import { NEXT_RSC_UNION_QUERY } from '../../../../client/components/app-router-headers'
/** /**
* Ensure only a-zA-Z are used for param names for proper interpolating * Ensure only a-zA-Z are used for param names for proper interpolating
...@@ -163,6 +164,7 @@ export function prepareDestination(args: { ...@@ -163,6 +164,7 @@ export function prepareDestination(args: {
delete query.__nextDefaultLocale delete query.__nextDefaultLocale
delete query.__nextDataReq delete query.__nextDataReq
delete query.__nextInferredLocaleFromDefault delete query.__nextInferredLocaleFromDefault
delete query[NEXT_RSC_UNION_QUERY]
let escapedDestination = args.destination let escapedDestination = args.destination
......
import { strict as assert } from 'node:assert' import { strict as assert } from 'node:assert'
import Link from 'next/link'
// @ts-ignore // @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers' import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
export default function Page({ searchParams }) { export default function Page({ searchParams }) {
assert(searchParams[NEXT_RSC_UNION_QUERY] === undefined) assert(searchParams[NEXT_RSC_UNION_QUERY] === undefined)
return <p>no rsc query page</p> return (
<>
<p>no rsc query page</p>
return{' '}
<Link id="link-to-pages" href="/some">
To /some
</Link>
</>
)
} }
...@@ -419,6 +419,38 @@ createNextDescribe( ...@@ -419,6 +419,38 @@ createNextDescribe(
}) })
}) })
describe('navigation between pages and app', () => {
it('should not contain _rsc query while navigating from app to pages', async () => {
// Initiate with app
const browser = await next.browser('/assertion/page')
await browser
.elementByCss('#link-to-pages')
.click()
.waitForElementByCss('#link-to-app')
expect(await browser.url()).toBe(next.url + '/some')
await browser
.elementByCss('#link-to-app')
.click()
.waitForElementByCss('#link-to-pages')
expect(await browser.url()).toBe(next.url + '/assertion/page')
})
it('should not contain _rsc query while navigating from pages to app', async () => {
// Initiate with pages
const browser = await next.browser('/some')
await browser
.elementByCss('#link-to-app')
.click()
.waitForElementByCss('#link-to-pages')
expect(await browser.url()).toBe(next.url + '/assertion/page')
await browser
.elementByCss('#link-to-pages')
.click()
.waitForElementByCss('#link-to-app')
expect(await browser.url()).toBe(next.url + '/some')
})
})
describe('nested navigation', () => { describe('nested navigation', () => {
it('should navigate to nested pages', async () => { it('should navigate to nested pages', async () => {
const browser = await next.browser('/nested-navigation') const browser = await next.browser('/nested-navigation')
......
import Link from 'next/link'
export default function page() {
return (
<Link id="link-to-app" href="/assertion/page">
To /assertion/page
</Link>
)
}
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