Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for URL objects in Link and Router #1345

Merged
merged 19 commits into from
Mar 12, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/link.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'url'
import { resolve, format } from 'url'
import React, { Component, Children, PropTypes } from 'react'
import Router from './router'
import { warn, execOnce, getLocationOrigin } from './utils'
Expand Down Expand Up @@ -39,6 +39,11 @@ export default class Link extends Component {
return
}

if (href && typeof href === 'object') {
// format href object to generate url
href = format(href)
}

const { pathname } = window.location
href = resolve(pathname, href)
as = as ? resolve(pathname, as) : href
Expand Down Expand Up @@ -83,7 +88,7 @@ export default class Link extends Component {
}

render () {
let { children } = this.props
let { children, as, href } = this.props
// Deprecated. Warning shown by propType check. If the childen provided is a string (<Link>example</Link>) we wrap it in an <a> tag
if (typeof children === 'string') {
children = <a>{children}</a>
Expand All @@ -97,7 +102,7 @@ export default class Link extends Component {

// If child is an <a> tag and doesn't have a href attribute we specify it so that repetition is not needed by the user
if (child.type === 'a' && !('href' in child.props)) {
props.href = this.props.as || this.props.href
props.href = as || (href && typeof href === 'object' ? format(href) : href)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happen if as is also an object. So, we need to first convert both as and href into the string version.
That's easy and less error prone.

Do this in the constructor and componentWillReceiveProps and save it to the this context. So, we can use it efficiently from everywhere in the link.

}

return React.cloneElement(child, props)
Expand Down
6 changes: 4 additions & 2 deletions lib/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ export default class Router extends EventEmitter {
}

push (url, as = url) {
return this.change('pushState', url, as)
const formattedUrl = typeof url === 'object' ? format(url) : url
return this.change('pushState', formattedUrl, as === url ? formattedUrl : as)
}

replace (url, as = url) {
return this.change('replaceState', url, as)
const formattedUrl = typeof url === 'object' ? format(url) : url
return this.change('replaceState', formattedUrl, as === url ? formattedUrl : as)
}

async change (method, url, as) {
Expand Down
36 changes: 36 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,24 @@ Each top-level component receives a `url` property with the following API:

The second `as` parameter for `push` and `replace` is an optional _decoration_ of the URL. Useful if you configured custom routes on the server.

##### With URL object
The component `<Link>` can also receive an URL object and it will automatically format it to create the URL string.

```jsx
// pages/index.js
import Link from 'next/link'
export default () => (
<div>Click <Link href={{ pathname: 'about', query: { name: 'Zeit' }}}<a>here</a></Link> to read more</div>
)
```

That will generate the URL string `/about?name=Zeit`, you can use the following parameters:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about linking to the existing doc you mentioned (https://nodejs.org/api/url.html#url_url_strings_and_url_objects) instead?

Are all the properties supported? What happens if user passes auth, host, etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can link the Node.js documentation yeah, that's probably better.

Yes, all properties work, you can create a URL right now using them manually, I don't think it's a problem, the only bug I watched about that is that if you don't set a protocol and set host and/or auth the isLocal function return true even if it's not 🤔


- `pathname` - `String` of the link path excluding the query string. If it doesn't start with `/` is automatically added
- `query` - `Object` with the parsed query string. Optional, if you set `search` this is ignored
- `hash` - `String` with the fragment to be used after a `#` character. Optional, if it doesn't start with `#` is automatically added
- `search` - `String` with the stringified query. Optional, if it doesn't start with `?` is automatically added

#### Imperatively

<p><details>
Expand Down Expand Up @@ -302,6 +320,24 @@ The second `as` parameter for `push` and `replace` is an optional _decoration_ o

_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_

##### With URL object
You can use an URL object the same way you use it in a `<Link>` component to `push` and `replace` an url.

```jsx
import Router from 'next/router'

const handler = () => Router.push({
pathname: 'about',
query: { name: 'Zeit' }
})

export default () => (
<div>Click <span onClick={handler}>here</span> to read more</div>
)
```

This uses of the same exact parameters as in the `<Link>` component.

##### Router Events

You can also listen to different events happening inside the Router.
Expand Down