-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the Singleton Router API (#429)
* 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.
- Loading branch information
Showing
14 changed files
with
217 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,13 @@ | ||
# Example app utilizing next/router for routing | ||
|
||
This example features: | ||
|
||
* An app linking pages using `next/router` instead of `<Link>` component. | ||
* Access the pathname using `next/router` and render it in a component | ||
|
||
## How to run it | ||
|
||
```sh | ||
npm install | ||
npm run dev | ||
``` |
This file contains 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,31 @@ | ||
import React from 'react' | ||
import Router from 'next/router' | ||
|
||
const styles = { | ||
a: { | ||
marginRight: 10 | ||
} | ||
} | ||
|
||
const Link = ({ children, href }) => ( | ||
<a | ||
href='#' | ||
style={styles.a} | ||
onClick={(e) => { | ||
e.preventDefault() | ||
Router.push(href) | ||
}} | ||
> | ||
{ children } | ||
</a> | ||
) | ||
|
||
export default () => ( | ||
<div> | ||
<Link href='/'>Home</Link> | ||
<Link href='/about'>About</Link> | ||
<div> | ||
<small>Now you are in the route: {Router.route} </small> | ||
</div> | ||
</div> | ||
) |
This file contains 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": "shared-modules", | ||
"version": "1.0.0", | ||
"description": "This example features:", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"next": "*" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
This file contains 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,9 @@ | ||
import React from 'react' | ||
import Header from '../components/Header' | ||
|
||
export default () => ( | ||
<div> | ||
<Header /> | ||
<p>This is the about page.</p> | ||
</div> | ||
) |
This file contains 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,9 @@ | ||
import React from 'react' | ||
import Header from '../components/Header' | ||
|
||
export default () => ( | ||
<div> | ||
<Header /> | ||
<p>HOME PAGE is here!</p> | ||
</div> | ||
) |
This file contains 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 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,51 @@ | ||
import _Router from './router' | ||
|
||
// holds the actual router instance | ||
let router = null | ||
|
||
const SingletonRouter = {} | ||
|
||
// Create public properties and methods of the router in the SingletonRouter | ||
const propertyFields = ['route', 'components', 'pathname', 'query'] | ||
const methodFields = ['push', 'replace', 'reload', 'back'] | ||
|
||
propertyFields.forEach((field) => { | ||
// Here we need to use Object.defineProperty because, we need to return | ||
// the property assigned to the actual router | ||
// The value might get changed as we change routes and this is the | ||
// proper way to access it | ||
Object.defineProperty(SingletonRouter, field, { | ||
get () { | ||
return router[field] | ||
} | ||
}) | ||
}) | ||
|
||
methodFields.forEach((field) => { | ||
SingletonRouter[field] = (...args) => { | ||
return router[field](...args) | ||
} | ||
}) | ||
|
||
// This is an internal method and it should not be called directly. | ||
// | ||
// ## Client Side Usage | ||
// We create the router in the client side only for a single time when we are | ||
// booting the app. It happens before rendering any components. | ||
// At the time of the component rendering, there'll be a router instance | ||
// | ||
// ## Server Side Usage | ||
// We create router for every SSR page render. | ||
// Since rendering happens in the same eventloop this works properly. | ||
export const createRouter = function (...args) { | ||
router = new _Router(...args) | ||
return router | ||
} | ||
|
||
// Export the actual Router class, which is also use internally | ||
// You'll ever need to access this directly | ||
export const Router = _Router | ||
|
||
// Export the SingletonRouter and this is the public API. | ||
// This is an client side API and doesn't available on the server | ||
export default SingletonRouter |
This file contains 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 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 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 @@ | ||
module.exports = require('./dist/lib/router') |
Oops, something went wrong.