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

Correct client rewrite resolving with query #16860

Merged
merged 2 commits into from
Sep 4, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions test/integration/custom-routes/pages/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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' })
Expand Down
28 changes: 28 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,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')
Expand Down
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!'
}