mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Add asPath on the server (#3149)
* Add asPath on the server * Make sure we don’t include `?` when there is no query
This commit is contained in:
parent
40bb56e892
commit
03324880a8
|
@ -70,7 +70,7 @@ async function doRender (req, res, pathname, query, {
|
||||||
const app = createElement(App, {
|
const app = createElement(App, {
|
||||||
Component: enhancer(Component),
|
Component: enhancer(Component),
|
||||||
props,
|
props,
|
||||||
router: new Router(pathname, query)
|
router: new Router(pathname, query, asPath)
|
||||||
})
|
})
|
||||||
|
|
||||||
const render = staticMarkup ? renderToStaticMarkup : renderToString
|
const render = staticMarkup ? renderToStaticMarkup : renderToString
|
||||||
|
|
|
@ -8,7 +8,8 @@ const Link = withRouter(({router, children, href}) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<span>Current path: {router.pathname}</span>
|
<span id='pathname'>Current path: {router.pathname}</span>
|
||||||
|
<span id='asPath'>Current asPath: {router.asPath}</span>
|
||||||
<a href='#' onClick={handleClick}>{children}</a>
|
<a href='#' onClick={handleClick}>{children}</a>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -407,8 +407,11 @@ export default (context, render) => {
|
||||||
it('should navigate as expected', async () => {
|
it('should navigate as expected', async () => {
|
||||||
const browser = await webdriver(context.appPort, '/nav/with-hoc')
|
const browser = await webdriver(context.appPort, '/nav/with-hoc')
|
||||||
|
|
||||||
const spanText = await browser.elementByCss('span').text()
|
const pathname = await browser.elementByCss('#pathname').text()
|
||||||
expect(spanText).toBe('Current path: /nav/with-hoc')
|
expect(pathname).toBe('Current path: /nav/with-hoc')
|
||||||
|
|
||||||
|
const asPath = await browser.elementByCss('#asPath').text()
|
||||||
|
expect(asPath).toBe('Current asPath: /nav/with-hoc')
|
||||||
|
|
||||||
const text = await browser
|
const text = await browser
|
||||||
.elementByCss('.nav-with-hoc a').click()
|
.elementByCss('.nav-with-hoc a').click()
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* global describe, test, expect */
|
/* global describe, test, it, expect */
|
||||||
|
|
||||||
import cheerio from 'cheerio'
|
import cheerio from 'cheerio'
|
||||||
|
|
||||||
|
@ -79,5 +79,19 @@ export default function ({ app }, suiteName, render) {
|
||||||
expect($('h1').text()).toBe('404')
|
expect($('h1').text()).toBe('404')
|
||||||
expect($('h2').text()).toBe('This page could not be found.')
|
expect($('h2').text()).toBe('This page could not be found.')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('with the HOC based router', () => {
|
||||||
|
it('should navigate as expected', async () => {
|
||||||
|
const $ = await get$('/nav/with-hoc')
|
||||||
|
|
||||||
|
expect($('#pathname').text()).toBe('Current path: /nav/with-hoc')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should include asPath', async () => {
|
||||||
|
const $ = await get$('/nav/with-hoc')
|
||||||
|
|
||||||
|
expect($('#asPath').text()).toBe('Current asPath: /nav/with-hoc')
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,13 +17,13 @@ export const nextBuild = build
|
||||||
export const nextExport = _export
|
export const nextExport = _export
|
||||||
export const pkg = _pkg
|
export const pkg = _pkg
|
||||||
|
|
||||||
export function renderViaAPI (app, pathname, query = {}) {
|
export function renderViaAPI (app, pathname, query) {
|
||||||
const url = `${pathname}?${qs.stringify(query)}`
|
const url = `${pathname}${query ? `?${qs.stringify(query)}` : ''}`
|
||||||
return app.renderToHTML({ url }, {}, pathname, query)
|
return app.renderToHTML({ url }, {}, pathname, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderViaHTTP (appPort, pathname, query = {}) {
|
export function renderViaHTTP (appPort, pathname, query) {
|
||||||
const url = `http://localhost:${appPort}${pathname}?${qs.stringify(query)}`
|
const url = `http://localhost:${appPort}${pathname}${query ? `?${qs.stringify(query)}` : ''}`
|
||||||
return fetch(url).then((res) => res.text())
|
return fetch(url).then((res) => res.text())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue