Skip to content

Commit

Permalink
Add pathname and query to the argument of getInitialProps (#194)
Browse files Browse the repository at this point in the history
* pass pathname and query to getInitialProps on both server and client

* README: describe about the contet object of getInitialProps
  • Loading branch information
nkzawa authored and rauchg committed Nov 4, 2016
1 parent e775721 commit d1a0309
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ export default class extends React.Component {

Notice that to load data when the page loads, we use `getInitialProps` which is an [`async`](https://zeit.co/blog/async-and-await) static method. It can asynchronously fetch anything that resolves to a JavaScript plain `Object`, which populates `props`.

`getInitialProps` receives a context object with the following properties:

- `pathname` - path section of URL
- `query` - query string section of URL parsed as an object
- `req` - HTTP request object (server only)
- `res` - HTTP response object (server only)
- `xhr` - XMLHttpRequest object (client only)
- `err` - Error object if any error is encountered during the rendering

### Routing

Client-side transitions between routes are enabled via a `<Link>` component
Expand Down
18 changes: 13 additions & 5 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ export default class Router {
onPopState (e) {
this.abortComponentLoad()

const route = (e.state || {}).route || toRoute(window.location.pathname)
const { pathname, query } = parse(window.location.href)
const route = (e.state || {}).route || toRoute(pathname)

Promise.resolve()
.then(async () => {
const data = await this.fetchComponent(route)
let props
if (route !== this.route) {
props = await this.getInitialProps(data.Component, data.ctx)
const ctx = { ...data.ctx, pathname, query }
props = await this.getInitialProps(data.Component, ctx)
}

this.route = route
Expand Down Expand Up @@ -64,12 +66,15 @@ export default class Router {

if (route !== this.route) return

const { pathname, query } = parse(window.location.href)

let data
let props
try {
data = await this.fetchComponent(route)
if (route === this.route) {
props = await this.getInitialProps(data.Component, data.ctx)
const ctx = { ...data.ctx, pathname, query }
props = await this.getInitialProps(data.Component, ctx)
}
} catch (err) {
if (err.cancelled) return false
Expand All @@ -92,7 +97,9 @@ export default class Router {
}

async change (method, route, url) {
if (!route) route = toRoute(parse(url).pathname)
const { pathname, query } = parse(url, true)

if (!route) route = toRoute(pathname)

this.abortComponentLoad()

Expand All @@ -101,7 +108,8 @@ export default class Router {
try {
data = await this.fetchComponent(route)
if (route !== this.route) {
props = await this.getInitialProps(data.Component, data.ctx)
const ctx = { ...data.ctx, pathname, query }
props = await this.getInitialProps(data.Component, ctx)
}
} catch (err) {
if (err.cancelled) return false
Expand Down
9 changes: 6 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export default class Server {

async render (req, res) {
const { dir, dev } = this
const ctx = { req, res }
const { pathname, query } = parse(req.url, true)
const ctx = { req, res, pathname, query }
const opts = { dir, dev }

let html
Expand Down Expand Up @@ -142,17 +143,19 @@ export default class Server {

async render404 (req, res) {
const { dir, dev } = this
const { pathname, query } = parse(req.url, true)
const ctx = { req, res, pathname, query }
const opts = { dir, dev }

let html

const err = this.getCompilationError('/_error')
if (err) {
res.statusCode = 500
html = await render('/_error-debug', { req, res, err }, opts)
html = await render('/_error-debug', { ...ctx, err }, opts)
} else {
res.statusCode = 404
html = await render('/_error', { req, res }, opts)
html = await render('/_error', ctx, opts)
}

sendHTML(res, html)
Expand Down

0 comments on commit d1a0309

Please sign in to comment.