Unverified Commit f19b31ad authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix rsc payload fetch failures due to state tree encoding (#51017)

Encode the state tree where the content could contain unicode when
request the RSC payload, to avoid the fetch failure due to bad encoding
for headers

Fixes #48728
fix NEXT-1258
parent 96e47d3f
......@@ -37,7 +37,9 @@ export async function fetchServerResponse(
// Enable flight response
[RSC]: '1',
// Provide the current router state
[NEXT_ROUTER_STATE_TREE]: JSON.stringify(flightRouterState),
[NEXT_ROUTER_STATE_TREE]: encodeURIComponent(
JSON.stringify(flightRouterState)
),
}
/**
......
......@@ -23,7 +23,9 @@ export function parseAndValidateFlightRouterState(
}
try {
return flightRouterStateSchema.parse(JSON.parse(stateHeader))
return flightRouterStateSchema.parse(
JSON.parse(decodeURIComponent(stateHeader))
)
} catch {
throw new Error('The router state header was sent but could not be parsed.')
}
......
import Link from 'next/link'
export default function page({ searchParams }) {
return (
<div>
<p id="name">{searchParams.name ?? ''}</p>
<Link id="link" href="/">
home
</Link>
</div>
)
}
import { createNextDescribe } from 'e2e-utils'
import webdriver from 'next-webdriver'
import { check } from 'next-test-utils'
import type { Request } from 'playwright-chromium'
createNextDescribe(
'app dir - navigation',
......@@ -10,7 +10,7 @@ createNextDescribe(
({ next, isNextDev, isNextDeploy }) => {
describe('query string', () => {
it('should set query correctly', async () => {
const browser = await webdriver(next.url, '/')
const browser = await next.browser('/')
expect(await browser.elementById('query').text()).toMatchInlineSnapshot(
`""`
)
......@@ -25,6 +25,35 @@ createNextDescribe(
const url = new URL(await browser.url())
expect(url.searchParams.toString()).toMatchInlineSnapshot(`"a=b&c=d"`)
})
it('should handle unicode search params', async () => {
const requests = []
const browser = await next.browser('/search-params?name=名')
browser.on('request', async (req: Request) => {
const res = await req.response()
requests.push([
new URL(req.url()).pathname,
res.ok(),
await res.headers(),
])
})
expect(await browser.elementById('name').text()).toBe('名')
await browser.elementById('link').click()
await check(async () => {
return requests.some((requestPair) => {
const [pathname, ok, headers] = requestPair
return (
pathname === '/' &&
ok &&
headers['content-type'] === 'text/x-component'
)
})
? 'success'
: JSON.stringify(requests)
}, 'success')
})
})
describe('hash', () => {
......
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