Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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)
),
}

/**
Expand Down
Expand Up @@ -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.')
}
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/app-dir/navigation/app/search-params/page.js
@@ -0,0 +1,12 @@
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>
)
}
33 changes: 31 additions & 2 deletions test/e2e/app-dir/navigation/navigation.test.ts
@@ -1,6 +1,6 @@
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',
Expand All @@ -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(
`""`
)
Expand All @@ -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', () => {
Expand Down