From b5ca4adb4104dc654ab9cff140fb3481e2bd2c26 Mon Sep 17 00:00:00 2001 From: Tobias Kronthaler Date: Sun, 30 Jul 2017 21:46:35 +0200 Subject: [PATCH] Server: Custom status codes (404 on NoMatch) --- server/universal.js | 2 +- src/components/NoMatch.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/server/universal.js b/server/universal.js index 5de8773..ef333ea 100644 --- a/server/universal.js +++ b/server/universal.js @@ -36,7 +36,7 @@ module.exports = function universalLoader(req, res) { } else { // we're good, send the response const RenderedApp = htmlData.replace('{{SSR}}', markup) - res.send(RenderedApp) + res.status(context.statusCode || 200).send(RenderedApp) } }) } diff --git a/src/components/NoMatch.js b/src/components/NoMatch.js index 7cbb8a2..09d0fba 100644 --- a/src/components/NoMatch.js +++ b/src/components/NoMatch.js @@ -1,7 +1,19 @@ import React, { Component } from 'react' +import PropTypes from 'prop-types' //import './App.css' class NoMatch extends Component { + static contextTypes = { + router: PropTypes.shape({ + staticContext: PropTypes.object + }).isRequired + } + + componentWillMount() { + if (this.context.router.staticContext) + this.context.router.staticContext.statusCode = 404 + } + render() { return (