-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Introducing Shallow Routing #1357
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
60d887f
Simplify route info handling.
arunoda 3f66f1e
Add basic resolve=false support.
arunoda a0d2bb4
Make sure to render getInitialProps always if it's the first render.
arunoda c86035d
Change resolve=false to shallow routing.
arunoda d22dd16
Add test cases for shallow routing.
arunoda da1b973
Update README for shallow routing docs.
arunoda d820e6f
Update docs.
arunoda 4b46357
Update docs.
arunoda 0adba3c
Update docs.
arunoda e0a04ec
Merge branch 'master' into shallow-routing
arunoda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
|
|
||
| # Shallow Routing Example | ||
|
|
||
| ## How to use | ||
|
|
||
| Download the example [or clone the repo](https://github.com/zeit/next.js): | ||
|
|
||
| ```bash | ||
| curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/hello-world | ||
| cd hello-world | ||
| ``` | ||
|
|
||
| Install it and run: | ||
|
|
||
| ```bash | ||
| npm install | ||
| npm run dev | ||
| ``` | ||
|
|
||
| Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) | ||
|
|
||
| ```bash | ||
| now | ||
| ``` | ||
|
|
||
| ## The idea behind the example | ||
|
|
||
| With shallow routing, we could change the URL without actually running the `getInitialProps` every time you change the URL. | ||
|
|
||
| We do this passing the `shallow: true` option to `Router.push` or `Router.replace`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "with-shallow-routing", | ||
| "version": "1.0.0", | ||
| "scripts": { | ||
| "dev": "next", | ||
| "build": "next build", | ||
| "start": "next start" | ||
| }, | ||
| "dependencies": { | ||
| "next": "next@beta", | ||
| "react": "^15.4.2", | ||
| "react-dom": "^15.4.2" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default () => ( | ||
| <div>About us</div> | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import React from 'react' | ||
| import Link from 'next/link' | ||
| import Router from 'next/router' | ||
| import { format } from 'url' | ||
|
|
||
| let counter = 1 | ||
|
|
||
| export default class Index extends React.Component { | ||
| static getInitialProps ({ res }) { | ||
| if (res) { | ||
| return { initialPropsCounter: 1 } | ||
| } | ||
|
|
||
| counter++ | ||
| return { | ||
| initialPropsCounter: counter | ||
| } | ||
| } | ||
|
|
||
| reload () { | ||
| const { pathname, query } = Router | ||
| Router.push(format({ pathname, query })) | ||
| } | ||
|
|
||
| incrementStateCounter () { | ||
| const { url } = this.props | ||
| const currentCounter = url.query.counter ? parseInt(url.query.counter) : 0 | ||
| const href = `/?counter=${currentCounter + 1}` | ||
| Router.push(href, href, { shallow: true }) | ||
| } | ||
|
|
||
| render () { | ||
| const { initialPropsCounter, url } = this.props | ||
|
|
||
| return ( | ||
| <div> | ||
| <h2>This is the Home Page</h2> | ||
| <Link href='/about'><a>About</a></Link> | ||
| <button onClick={() => this.reload()}>Reload</button> | ||
| <button onClick={() => this.incrementStateCounter()}>Change State Counter</button> | ||
| <p>"getInitialProps" ran for "{initialPropsCounter}" times.</p> | ||
| <p>Counter: "{url.query.counter || 0}".</p> | ||
| </div> | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are the conditions before deciding to use shallow routing.