Skip to content

Commit 0332488

Browse files
timneutkensrauchg
authored andcommitted
Add asPath on the server (vercel#3149)
* Add asPath on the server * Make sure we don’t include `?` when there is no query
1 parent 40bb56e commit 0332488

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

server/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function doRender (req, res, pathname, query, {
7070
const app = createElement(App, {
7171
Component: enhancer(Component),
7272
props,
73-
router: new Router(pathname, query)
73+
router: new Router(pathname, query, asPath)
7474
})
7575

7676
const render = staticMarkup ? renderToStaticMarkup : renderToString

test/integration/basic/pages/nav/with-hoc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const Link = withRouter(({router, children, href}) => {
88

99
return (
1010
<div>
11-
<span>Current path: {router.pathname}</span>
11+
<span id='pathname'>Current path: {router.pathname}</span>
12+
<span id='asPath'>Current asPath: {router.asPath}</span>
1213
<a href='#' onClick={handleClick}>{children}</a>
1314
</div>
1415
)

test/integration/basic/test/client-navigation.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,11 @@ export default (context, render) => {
407407
it('should navigate as expected', async () => {
408408
const browser = await webdriver(context.appPort, '/nav/with-hoc')
409409

410-
const spanText = await browser.elementByCss('span').text()
411-
expect(spanText).toBe('Current path: /nav/with-hoc')
410+
const pathname = await browser.elementByCss('#pathname').text()
411+
expect(pathname).toBe('Current path: /nav/with-hoc')
412+
413+
const asPath = await browser.elementByCss('#asPath').text()
414+
expect(asPath).toBe('Current asPath: /nav/with-hoc')
412415

413416
const text = await browser
414417
.elementByCss('.nav-with-hoc a').click()

test/integration/basic/test/rendering.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global describe, test, expect */
1+
/* global describe, test, it, expect */
22

33
import cheerio from 'cheerio'
44

@@ -79,5 +79,19 @@ export default function ({ app }, suiteName, render) {
7979
expect($('h1').text()).toBe('404')
8080
expect($('h2').text()).toBe('This page could not be found.')
8181
})
82+
83+
describe('with the HOC based router', () => {
84+
it('should navigate as expected', async () => {
85+
const $ = await get$('/nav/with-hoc')
86+
87+
expect($('#pathname').text()).toBe('Current path: /nav/with-hoc')
88+
})
89+
90+
it('should include asPath', async () => {
91+
const $ = await get$('/nav/with-hoc')
92+
93+
expect($('#asPath').text()).toBe('Current asPath: /nav/with-hoc')
94+
})
95+
})
8296
})
8397
}

test/lib/next-test-utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export const nextBuild = build
1717
export const nextExport = _export
1818
export const pkg = _pkg
1919

20-
export function renderViaAPI (app, pathname, query = {}) {
21-
const url = `${pathname}?${qs.stringify(query)}`
20+
export function renderViaAPI (app, pathname, query) {
21+
const url = `${pathname}${query ? `?${qs.stringify(query)}` : ''}`
2222
return app.renderToHTML({ url }, {}, pathname, query)
2323
}
2424

25-
export function renderViaHTTP (appPort, pathname, query = {}) {
26-
const url = `http://localhost:${appPort}${pathname}?${qs.stringify(query)}`
25+
export function renderViaHTTP (appPort, pathname, query) {
26+
const url = `http://localhost:${appPort}${pathname}${query ? `?${qs.stringify(query)}` : ''}`
2727
return fetch(url).then((res) => res.text())
2828
}
2929

0 commit comments

Comments
 (0)