From 8099a4b45e691340c3e4aea4a3cc63e732c53342 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Wed, 6 Nov 2019 23:55:21 +0100 Subject: [PATCH] Fix bug mixing props on 404 --- src/components/NotFoundPage.js | 4 ++-- src/components/NotFoundWrapper.js | 24 ++++++++++++++++++++++- src/containers/MainContainer.js | 2 -- src/containers/PostContainer.js | 2 -- src/utils/serverRender.js | 32 ++++++++++++++++++++----------- 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/components/NotFoundPage.js b/src/components/NotFoundPage.js index 628825d..dce0428 100644 --- a/src/components/NotFoundPage.js +++ b/src/components/NotFoundPage.js @@ -3,10 +3,10 @@ import { Navbar, Header } from '.' import '../stylesheets/globals.scss' import contentStyle from '../stylesheets/content.scss' -export const NotFoundPage = () => { +export const NotFoundPage = (props) => { return (
- +
diff --git a/src/components/NotFoundWrapper.js b/src/components/NotFoundWrapper.js index f36c363..902f863 100644 --- a/src/components/NotFoundWrapper.js +++ b/src/components/NotFoundWrapper.js @@ -1,12 +1,34 @@ import React, { Component } from 'react' import { Wrapper, NotFoundPage } from '.' +import PropTypes from 'prop-types' import '../stylesheets/globals.scss' export default class NotFoundWrapper extends Component { + static propTypes = { + config: PropTypes.object.isRequired + } + + constructor (props) { + super(props) + + let data + + if (__isBrowser__) { + data = window.__INITIAL_DATA__ + delete window.__INITIAL_DATA__ + } else { + data = props.staticContext.context + } + + this.state = { + config: data[1] + } + } + render () { return ( - + ) } diff --git a/src/containers/MainContainer.js b/src/containers/MainContainer.js index abd7b15..a531cb9 100644 --- a/src/containers/MainContainer.js +++ b/src/containers/MainContainer.js @@ -14,11 +14,9 @@ export default class MainContainer extends Component { // eslint-disable-next-line no-undef if (__isBrowser__) { data = window.__INITIAL_DATA__ - console.log("window:", data) delete window.__INITIAL_DATA__ } else { data = props.staticContext.context - console.log("StaticContext:", data) } this.state = { diff --git a/src/containers/PostContainer.js b/src/containers/PostContainer.js index 2ed28ff..77992cf 100644 --- a/src/containers/PostContainer.js +++ b/src/containers/PostContainer.js @@ -14,11 +14,9 @@ export default class PostContainer extends Component { // eslint-disable-next-line no-undef if (__isBrowser__) { data = window.__INITIAL_DATA__ - console.log("window:", data) delete window.__INITIAL_DATA__ } else { data = props.staticContext.context - console.log("StaticContext:", data) } this.state = { diff --git a/src/utils/serverRender.js b/src/utils/serverRender.js index e465486..4ea09a0 100644 --- a/src/utils/serverRender.js +++ b/src/utils/serverRender.js @@ -15,24 +15,34 @@ export class ServerRenderer { render (req, res, next) { - const activeRoute = routes.find((route) => matchPath(req.url, route)) || {} + const activeRoute = routes.find((route) => matchPath(req.url, route)) || false const head = this.head const config = this.config - - const promise = activeRoute.getData - ? activeRoute.getData(req.path) - : Promise.resolve() - - promise.then((data) => { - const context = [data, config] + + if (!activeRoute) { + const context = [{}, config] const markup = renderToString( ) - - res.status(200).send(renderFullPage(markup, head, data, config)) - }).catch(next) + res.status(404).send(renderFullPage(markup, head, {}, config)) + } else { + const promise = activeRoute.getData + ? activeRoute.getData(req.path) + : Promise.resolve() + + promise.then((data) => { + const context = [data, config] + const markup = renderToString( + + + + ) + + res.status(200).send(renderFullPage(markup, head, data, config)) + }).catch(next) + } } }