Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct client rewrite resolving with query (vercel#16860)
Browse files Browse the repository at this point in the history
This makes sure we only pass the as value's `pathname` instead of the full value so that we don't accidentally include `query` values while resolving the rewrites. This also adds tests to ensure the rewrites are resolved with the correct query values when only providing `href` and when manually mapping them with `href` and `as`

Fixes: vercel#16825
ijjk authored and Piotr Bosak committed Sep 26, 2020
1 parent 4def0a2 commit da197dd
Showing 5 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
@@ -597,7 +597,7 @@ export default class Router implements BaseRouter {

if (process.env.__NEXT_HAS_REWRITES) {
resolvedAs = resolveRewrites(
as,
parseRelativeUrl(as).pathname,
pages,
basePath,
rewrites,
19 changes: 19 additions & 0 deletions test/integration/custom-routes/pages/nav.js
Original file line number Diff line number Diff line change
@@ -6,8 +6,27 @@ export default () => (
<Link href="/hello" as="/first">
<a id="to-hello">to hello</a>
</Link>
<br />
<Link href="/hello-again" as="/second">
<a id="to-hello-again">to hello-again</a>
</Link>
<br />
<Link
href={{
pathname: '/with-params',
query: {
something: 1,
another: 'value',
},
}}
as="/params/1?another=value"
>
<a id="to-params-manual">to params (manual)</a>
</Link>
<br />
<Link href="/params/1?another=value">
<a id="to-params">to params</a>
</Link>
<br />
</>
)
2 changes: 1 addition & 1 deletion test/integration/custom-routes/pages/with-params.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { useRouter } from 'next/router'

const Page = () => {
const { query } = useRouter()
return <p>{JSON.stringify(query)}</p>
return <p id="query">{JSON.stringify(query)}</p>
}

Page.getInitialProps = () => ({ hello: 'GIPGIP' })
28 changes: 28 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
@@ -341,6 +341,34 @@ const runTests = (isDev = false) => {
expect(await getBrowserBodyText(browser)).toMatch(/Hello again/)
})

it('should work with rewrite when manually specifying href/as', async () => {
const browser = await webdriver(appPort, '/nav')
await browser
.elementByCss('#to-params-manual')
.click()
.waitForElementByCss('#query')

const query = JSON.parse(await browser.elementByCss('#query').text())
expect(query).toEqual({
something: '1',
another: 'value',
})
})

it('should work with rewrite when only specifying href', async () => {
const browser = await webdriver(appPort, '/nav')
await browser
.elementByCss('#to-params')
.click()
.waitForElementByCss('#query')

const query = JSON.parse(await browser.elementByCss('#query').text())
expect(query).toEqual({
something: '1',
another: 'value',
})
})

it('should match a page after a rewrite', async () => {
const html = await renderViaHTTP(appPort, '/to-hello')
expect(html).toContain('Hello')
4 changes: 3 additions & 1 deletion test/integration/dynamic-routing/pages/another.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default () => 'hello from another!'
export default function Another() {
return 'hello from another!'
}

0 comments on commit da197dd

Please sign in to comment.