Keep the variants prefix on payload requests the proxy rewrote

For a payload request, the proxy adapter appends the client's `_rsc`
parameter to the rewritten destination when the proxy's target does not
carry it. That block read the destination captured before the variants
code ran, so where the proxy had matched a static combination and
prefixed the destination, the block wrote the unprefixed destination
back. The request then reached the route without its combination, and a
variant read failed for a value that had been resolved. The block now
reads the rewrite header again after the prefix went on.

The test navigates through a link to a route the proxy rewrites, with a
target that drops the query, to a page that declares combinations. The
prefetch has to carry the baked value, and the navigation is then served
from the prefetch without a request.
parent 6ae4ff03
......@@ -620,9 +620,14 @@ export async function adapter(
* the external server does not have access to the original URL or its search parameters.
* In these cases, forwarding the `_rsc` parameter is essential so that the external server
* can perform the correct RSC hash validation.
*
* This reads the rewrite header again rather than the value captured above,
* because the variants code above can replace the destination with a prefixed
* one.
*/
if (response && rewrite && isRSCRequest && rscHash) {
const rewriteURL = new URL(rewrite)
const finalRewrite = response?.headers.get('x-middleware-rewrite')
if (response && finalRewrite && isRSCRequest && rscHash) {
const rewriteURL = new URL(finalRewrite)
if (!rewriteURL.searchParams.has(NEXT_RSC_UNION_QUERY)) {
rewriteURL.searchParams.set(NEXT_RSC_UNION_QUERY, rscHash)
response.headers.set('x-middleware-rewrite', rewriteURL.toString())
......
......@@ -12,6 +12,9 @@ export default function Page({
<UnenumeratedParamLink searchParams={searchParams} />
</Suspense>
<LinkAccordion href="/on-demand/built">Enumerated param</LinkAccordion>
<LinkAccordion href="/rewrite-without-query">
Rewritten without query
</LinkAccordion>
</>
)
}
......
......@@ -37,6 +37,17 @@ export const proxy = wrapProxy(variantsByRoute, (request: NextRequest) => {
return NextResponse.rewrite(target)
}
if (pathname === '/rewrite-without-query') {
// The target drops the query, and `/paramless` declares combinations. A
// payload request then makes Next.js add its `_rsc` parameter to the
// rewritten destination after the variants prefix went on.
const target = request.nextUrl.clone()
target.pathname = '/paramless'
target.search = ''
return NextResponse.rewrite(target)
}
if (pathname === '/external') {
const port = process.env.EXTERNAL_SERVER_PORT
......@@ -54,6 +65,7 @@ export const config = {
matcher: [
'/',
'/rewrite-source',
'/rewrite-without-query',
'/external',
'/conditional-runtime/:slug',
'/enumerated/:slug',
......
import { isNextDeploy, isNextStart, nextTestSetup } from 'e2e-utils'
import { isNextDeploy, isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
import type * as Playwright from 'playwright'
import { createRouterAct } from '../../../lib/router-act'
import { findPort, retry } from 'next-test-utils'
......@@ -146,6 +146,76 @@ describe('variants', () => {
expect(await browser.eval('location.pathname')).toBe(url('/rewrite-source'))
})
it('should keep the combination on a navigation the proxy rewrote without its query', async () => {
// The client router requests a payload with `_rsc`. The proxy's target
// drops the query, so Next.js adds the parameter to the rewritten
// destination again. It has to add it to the prefixed destination, or the
// combination is lost and the route renders without the values it was
// resolved for.
let page: Playwright.Page | undefined
const browser = await next.browser(url('/prefetch-hub'), {
async beforePageLoad(p: Playwright.Page) {
page = p
await p
.context()
.addCookies([{ name: 'theme', value: 'dark', url: next.url }])
},
})
if (!page) {
throw new Error('The page was not captured before it loaded.')
}
// The value is baked into the artifact of the combination, so it is in the
// shell of the route, which arrives in an app-shell prefetch where the
// router prefetches at all.
const act = createRouterAct(page, { includeAppShellRequests: true })
// A value behind a boundary reaches the payload as a row the boundary
// refers to, rather than inline, so this matches the row.
const combination = { includes: '"dark"\n' }
const toggle = async () => {
const checkbox = await browser.elementByCss(
'input[data-link-accordion="/rewrite-without-query"]'
)
await checkbox.click()
}
const navigate = async () => {
const link = await browser.elementByCss(
`a[href="${url('/rewrite-without-query')}"]`
)
await link.click()
}
if (isNextDev) {
// A link does not prefetch in development, so the navigation is the one
// request that carries the value.
await act(async () => {
await toggle()
await navigate()
}, combination)
} else {
// The prefetch carries the value. The route has no dynamic hole for this
// combination, so the navigation is served from what the prefetch cached
// and requests nothing.
await act(toggle, combination)
await act(navigate, 'no-requests')
}
await retry(async () => {
expect(await browser.elementByCss('#theme').text()).toBe('dark')
})
expect(await browser.eval('location.pathname')).toBe(
url('/rewrite-without-query')
)
})
it('should resolve enumerated variants on a prerendered route', async () => {
const dark = await next.browser(url('/enumerated/a'), {
async beforePageLoad(page: Playwright.Page) {
......
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