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
Add support for URL objects in Link and Router (vercel#1345)
* Add support for URL objects in Link and Router
* Fix typo in comment
* Fix possible bug if the `href` prop is `null`
* Document the usage of URL objects in Link and Router
* Update readme.md
* Parse URL to get the host & hostname in `isLocal`
This should check if the current location and the checked URL have the same `host` or `hostname`.
* Format `as` parameter from object to string if required
* Format `href` and `as` inside the construct and componentWillReceiveProps
* Use `JSON.stringify` to compare objects
* Add usage example
* chore(package): update chromedriver to version 2.28.0 (vercel#1386)
https://greenkeeper.io/
* Refactor the codebase a bit.
* Change the example name.
* Add a few test cases.
* Add the example to the README.
Download the example [or clone the repo](https://github.com/zeit/next.js):
6
+
7
+
```bash
8
+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-url-object-routing
9
+
cd with-url-object-routing
10
+
```
11
+
12
+
Install it and run:
13
+
14
+
```bash
15
+
npm install
16
+
npm run dev
17
+
```
18
+
19
+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
20
+
21
+
```bash
22
+
now
23
+
```
24
+
25
+
## The idea behind the example
26
+
27
+
Next.js allows using [Node.js URL objects](https://nodejs.org/api/url.html#url_url_strings_and_url_objects) as `href` and `as` values for `<Link>` component and parameters of `Router#push` and `Router#replace`.
28
+
29
+
This simplify the usage of parameterized URLs when you have many query values.
That will generate the URL string `/about?name=Zeit`, you can use every property as defined in the [Node.js URL module documentation](https://nodejs.org/api/url.html#url_url_strings_and_url_objects).
294
+
274
295
#### Imperatively
275
296
276
297
<p><details>
@@ -303,6 +324,24 @@ The second `as` parameter for `push` and `replace` is an optional _decoration_ o
303
324
304
325
_Note: in order to programmatically change the route without triggering navigation and component-fetching, use `props.url.push` and `props.url.replace` within a component_
305
326
327
+
##### With URL object
328
+
You can use an URL object the same way you use it in a `<Link>` component to `push` and `replace` an url.
329
+
330
+
```jsx
331
+
importRouterfrom'next/router'
332
+
333
+
consthandler= () =>Router.push({
334
+
pathname:'about',
335
+
query: { name:'Zeit' }
336
+
})
337
+
338
+
exportdefault () => (
339
+
<div>Click <span onClick={handler}>here</span> to read more</div>
340
+
)
341
+
```
342
+
343
+
This uses of the same exact parameters as in the `<Link>` component.
344
+
306
345
##### Router Events
307
346
308
347
You can also listen to different events happening inside the Router.
0 commit comments