From b044f4233482fe710703631a3e81fdc4c5886550 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 9 Dec 2021 14:45:22 +0100 Subject: [PATCH] Fix RSC link navigation (#32303) Co-authored-by: JJ Kasper Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/next/client/index.tsx | 6 ++---- packages/next/client/page-loader.ts | 4 +++- .../app/pages/next-api/link.server.js | 18 +++++++++++++----- .../test/index.test.js | 10 +++++++++- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/next/client/index.tsx b/packages/next/client/index.tsx index cf6d3fbac11a0..85a902d32443f 100644 --- a/packages/next/client/index.tsx +++ b/packages/next/client/index.tsx @@ -673,11 +673,9 @@ if (process.env.__NEXT_RSC) { return Promise.resolve({ body: t.readable }) })() : (() => { - const search = location.search + const { search, pathname } = location const flightReqUrl = - location.pathname + - search + - (search ? '&__flight__' : '?__flight__') + pathname + search + (search ? '&' : '?') + '__flight__' return fetch(flightReqUrl) })() ) diff --git a/packages/next/client/page-loader.ts b/packages/next/client/page-loader.ts index b9a768a2f1c8e..d4119a3b2767c 100644 --- a/packages/next/client/page-loader.ts +++ b/packages/next/client/page-loader.ts @@ -147,7 +147,9 @@ export default class PageLoader { const route = normalizeRoute(hrefPathname) const getHrefForSlug = (path: string) => { - if (rsc) return path + '?__flight__' + if (rsc) { + return path + search + (search ? `&` : '?') + '__flight__' + } const dataRoute = getAssetPathFromRoute( removePathTrailingSlash(addLocale(path, locale)), diff --git a/test/integration/react-streaming-and-server-components/app/pages/next-api/link.server.js b/test/integration/react-streaming-and-server-components/app/pages/next-api/link.server.js index 6258cbf2059a5..a0e988f015f2c 100644 --- a/test/integration/react-streaming-and-server-components/app/pages/next-api/link.server.js +++ b/test/integration/react-streaming-and-server-components/app/pages/next-api/link.server.js @@ -1,9 +1,17 @@ -import NextLink from 'next/link' +import Link from 'next/link' -export default function Link() { +export default function LinkPage({ router }) { + const { query } = router + const id = parseInt(query.id || '0', 10) return ( - - go home - + <> +

query:{id}

+ + next id + + + go home + + ) } diff --git a/test/integration/react-streaming-and-server-components/test/index.test.js b/test/integration/react-streaming-and-server-components/test/index.test.js index ad03ceb4b3152..c1ee90b3b61c1 100644 --- a/test/integration/react-streaming-and-server-components/test/index.test.js +++ b/test/integration/react-streaming-and-server-components/test/index.test.js @@ -14,6 +14,7 @@ import { nextBuild as _nextBuild, nextStart as _nextStart, renderViaHTTP, + check, } from 'next-test-utils' import css from './css' @@ -285,12 +286,19 @@ async function runBasicTests(context, env) { expect(pathNotFoundHTML).toContain('custom-404-page') }) - it('should suspense next/link on server side', async () => { + it('should support next/link', async () => { const linkHTML = await renderViaHTTP(context.appPort, '/next-api/link') const $ = cheerio.load(linkHTML) const linkText = $('div[hidden] > a[href="/"]').text() expect(linkText).toContain('go home') + + const browser = await webdriver(context.appPort, '/next-api/link') + await browser.eval('window.beforeNav = 1') + await browser.elementByCss('#next_id').click() + await browser.elementByCss('#next_id').click() + await check(() => browser.waitForElementByCss('#query').text(), /query:2/) + expect(await browser.eval('window.beforeNav')).toBe(1) }) it('should suspense next/image on server side', async () => {