You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Immplement the initial singleton Router.
* Use the new SingletonRouter for HMR error handling.
* Use SingletonRouter inside the Link.
* Create an example app using the Router.
* Make the url parameter optional in Router.push and Router.replace
* Add a section about next/router in the README.
Copy file name to clipboardexpand all lines: README.md
+50-7
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ and add a script to your package.json like this:
22
22
{
23
23
"scripts": {
24
24
"dev": "next"
25
-
}
25
+
}
26
26
}
27
27
```
28
28
@@ -142,9 +142,9 @@ For the initial page load, `getInitialProps` will execute on the server only. `g
142
142
-`xhr` - XMLHttpRequest object (client only)
143
143
-`err` - Error object if any error is encountered during the rendering
144
144
145
-
### Routing
145
+
### Routing with <Link>
146
146
147
-
Client-side transitions between routes are enabled via a `<Link>` component
147
+
Client-side transitions between routes can be enabled via a `<Link>` component
148
148
149
149
#### pages/index.js
150
150
@@ -178,11 +178,54 @@ Each top-level component receives a `url` property with the following API:
178
178
-`pushTo(url)` - performs a `pushState` call that renders the new `url`. This is equivalent to following a `<Link>`
179
179
-`replaceTo(url)` - performs a `replaceState` call that renders the new `url`
180
180
181
+
### Routing with next/router
182
+
183
+
You can also do client-side page transitions using the `next/router`. This is the same API used inside the above `<Link />` component.
184
+
185
+
```jsx
186
+
importRouterfrom'next/router'
187
+
188
+
constrouteTo(href) {
189
+
return (e) => {
190
+
e.preventDefault()
191
+
Router.push(href)
192
+
}
193
+
}
194
+
195
+
exportdefault () => (
196
+
<div>Click <a href='#' onClick={routeTo('/about')}>here</a> to read more</div>
197
+
)
198
+
```
199
+
200
+
#### pages/about.js
201
+
202
+
```jsx
203
+
exportdefault () => (
204
+
<p>Welcome to About!</p>
205
+
)
206
+
```
207
+
208
+
Above `Router` object comes with the following API:
209
+
210
+
-`route` - `String` of the current route
211
+
-`pathname` - `String` of the current path excluding the query string
212
+
-`query` - `Object` with the parsed query string. Defaults to `{}`
213
+
-`push(url, pathname=url)` - performs a `pushState` call associated with the current component
214
+
-`replace(url, pathname=url)` - performs a `replaceState` call associated with the current component
215
+
216
+
> Usually, route is the same as pathname.
217
+
> But when used with programmatic API, route and pathname can be different.
218
+
> "route" is your actual page's path while "pathname" is the path of the url mapped to it.
219
+
>
220
+
> Likewise, url and path is the same usually.
221
+
> But when used with programmatic API, "url" is the route with the query string.
222
+
> "pathname" is the path of the url mapped to it.
223
+
181
224
### Prefetching Pages
182
225
183
-
Next.js exposes a module that configures a `ServiceWorker` automatically to prefetch pages: `next/prefetch`.
226
+
Next.js exposes a module that configures a `ServiceWorker` automatically to prefetch pages: `next/prefetch`.
184
227
185
-
Since Next.js server-renders your pages, this allows all the future interaction paths of your app to be instant. Effectively Next.js gives you the great initial download performance of a _website_, with the ahead-of-time download capabilities of an _app_. [Read more](https://zeit.co/blog/next#anticipation-is-the-key-to-performance).
228
+
Since Next.js server-renders your pages, this allows all the future interaction paths of your app to be instant. Effectively Next.js gives you the great initial download performance of a _website_, with the ahead-of-time download capabilities of an _app_. [Read more](https://zeit.co/blog/next#anticipation-is-the-key-to-performance).
For custom advanced behavior of Next.js, you can create a `next.config.js` in the root of your project directory (next to `pages/` and `package.json`).
297
+
For custom advanced behavior of Next.js, you can create a `next.config.js` in the root of your project directory (next to `pages/` and `package.json`).
255
298
256
299
Note: `next.config.js` is a regular Node.js module, not a JSON file. It gets used by the Next server and build phases, and not included in the browser build.
257
300
@@ -264,7 +307,7 @@ module.exports = {
264
307
265
308
### Customizing webpack config
266
309
267
-
In order to extend our usage of `webpack`, you can define a function that extends its config.
310
+
In order to extend our usage of `webpack`, you can define a function that extends its config.
268
311
269
312
The following example shows how you can use [`react-svg-loader`](https://github.com/boopathi/react-svg-loader) to easily import any `.svg` file as a React component, without modification.
0 commit comments