Skip to content

Commit

Permalink
Provide response code for SSR via react component
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyzu committed Apr 10, 2018
1 parent 1103498 commit 0d63df7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react-cookie": "^2.0.7",
"react-dom": "^15.5.4",
"react-loadable": "^3.3.1",
"react-side-effect": "^1.1.5",
"react-redux": "^5.0.6",
"react-router": "^4.1.1",
"react-tree-walker": "^2.1.1",
Expand All @@ -45,4 +46,4 @@
"lint": "rispa-eslint .",
"lint:fix": "rispa-eslint . --fix"
}
}
}
1 change: 1 addition & 0 deletions response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/response')
29 changes: 29 additions & 0 deletions src/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Children, Component } from 'react'
import { node, number } from 'prop-types'
import withSideEffect from 'react-side-effect'

@withSideEffect(
propsList => {
const props = propsList[propsList.length - 1]
if (props) {
return props.statusCode
}

return undefined
},
() => {},
)
export default class Response extends Component {
static propTypes = {
children: node,
statusCode: number,
}

render() {
if (this.props.children) {
return Children.only(this.props.children)
}

return null
}
}
9 changes: 8 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import getRoutes from '@rispa/routes'
import { CookiesProvider } from 'react-cookie'
import { flushWebpackRequireWeakIds } from 'react-loadable'
import Response from '../response'
import Html from './Html'

let stats
Expand Down Expand Up @@ -113,7 +114,13 @@ const createRender = (assets, cacheConfig) => (req, res, config) => {
/>,
)}`

res.send(html)
const statusCode = Response.peek() || 200

Response.rewind()

res
.status(statusCode)
.send(html)
})
}

Expand Down

0 comments on commit 0d63df7

Please sign in to comment.