Skip to content

Commit dd33235

Browse files
committed
Make next/router a client only API.
1 parent 84a4dcd commit dd33235

File tree

3 files changed

+6
-24
lines changed

3 files changed

+6
-24
lines changed

examples/using-router/components/Header.js

-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,5 @@ export default () => (
2525
<Link href='/'>Home</Link>
2626
<Link href='/about'>About</Link>
2727
<Link href='/error'>Error</Link>
28-
<div>
29-
<small>Now you are in the route: {Router.route} </small>
30-
</div>
3128
</div>
3229
)

lib/router/index.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let router = null
66
const SingletonRouter = {}
77

88
// Create public properties and methods of the router in the SingletonRouter
9-
const propertyFields = ['route', 'components', 'pathname', 'query']
9+
const propertyFields = ['components', 'pathname', 'route', 'query']
1010
const methodFields = ['push', 'replace', 'reload', 'back']
1111

1212
propertyFields.forEach((field) => {
@@ -32,7 +32,7 @@ methodFields.forEach((field) => {
3232
function throwIfNoRouter () {
3333
if (!router) {
3434
const message = 'No router instance found.\n' +
35-
'If you are using "next/router" inside "getInitialProps", avoid it.\n'
35+
'You should only use "next/router" inside the client side of your app.\n'
3636
throw new Error(message)
3737
}
3838
}
@@ -52,17 +52,5 @@ export const createRouter = function (...args) {
5252
return router
5353
}
5454

55-
// This helper is used to assign a router instance only when running the "cb"
56-
// This is useful for assigning a router instance when we do SSR.
57-
export const withRouter = function (r, cb) {
58-
const original = router
59-
router = r
60-
const result = cb()
61-
62-
router = original
63-
return result
64-
}
65-
66-
// Export the actual Router class, which is also use internally
67-
// You'll ever need to access this directly
55+
// Export the actual Router class, which is usually used inside the server
6856
export const Router = _Router

server/render.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createElement } from 'react'
33
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
44
import requireModule from './require'
55
import read from './read'
6-
import { Router, withRouter } from '../lib/router'
6+
import { Router } from '../lib/router'
77
import Head, { defaultHead } from '../lib/head'
88
import App from '../lib/app'
99

@@ -56,16 +56,13 @@ async function doRender (req, res, pathname, query, {
5656
if (res.finished) return
5757

5858
const renderPage = () => {
59-
const router = new Router(pathname, query)
6059
const app = createElement(App, {
6160
Component,
6261
props,
63-
router
62+
router: new Router(pathname, query)
6463
})
6564

66-
const html = withRouter(router, () => {
67-
return (staticMarkup ? renderToStaticMarkup : renderToString)(app)
68-
})
65+
const html = (staticMarkup ? renderToStaticMarkup : renderToString)(app)
6966
const head = Head.rewind() || defaultHead()
7067
return { html, head }
7168
}

0 commit comments

Comments
 (0)