This repository has been archived by the owner on Nov 15, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
62 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,47 @@ | ||
import React from 'react'; | ||
import RouterContext from 'react-router/lib/RouterContext'; | ||
import createMemoryHistory from 'react-router/lib/createMemoryHistory'; | ||
import match from 'react-router/lib/match'; | ||
import render from '../htmlPage/render'; | ||
import routes from '../../shared/routes'; | ||
import React from "react" | ||
import RouterContext from "react-router/lib/RouterContext" | ||
import createMemoryHistory from "react-router/lib/createMemoryHistory" | ||
import match from "react-router/lib/match" | ||
import render from "../htmlPage/render" | ||
import routes from "../../shared/routes" | ||
|
||
/** | ||
* An express middleware that is capabable of doing React server side rendering. | ||
*/ | ||
function universalReactAppMiddleware(request, response) { | ||
if (process.env.DISABLE_SSR) { | ||
if (process.env.NODE_ENV === 'development') { | ||
console.log('==> 🐌 Handling react route without SSR'); // eslint-disable-line no-console | ||
if (process.env.NODE_ENV === "development") { | ||
console.log("==> 🐌 Handling react route without SSR") // eslint-disable-line no-console | ||
} | ||
|
||
// SSR is disabled so we will just return an empty html page and will | ||
// rely on the client to populate the initial react application state. | ||
const html = render(); | ||
response.status(200).send(html); | ||
return; | ||
const html = render() | ||
response.status(200).send(html) | ||
return | ||
} | ||
|
||
const history = createMemoryHistory(request.originalUrl); | ||
const history = createMemoryHistory(request.originalUrl) | ||
|
||
// Server side handling of react-router. | ||
// Read more about this here: | ||
// https://github.com/reactjs/react-router/blob/master/docs/guides/ServerRendering.md | ||
match({ routes, history }, (error, redirectLocation, renderProps) => { | ||
if (error) { | ||
response.status(500).send(error.message); | ||
response.status(500).send(error.message) | ||
} else if (redirectLocation) { | ||
response.redirect(302, redirectLocation.pathname + redirectLocation.search); | ||
response.redirect(302, redirectLocation.pathname + redirectLocation.search) | ||
} else if (renderProps) { | ||
// You can check renderProps.components or renderProps.routes for | ||
// your "not found" component or route respectively, and send a 404 as | ||
// below, if you're using a catch-all route. | ||
|
||
const html = render({ rootElement: <RouterContext {...renderProps} /> }); | ||
response.status(200).send(html); | ||
const html = render({ rootElement: <RouterContext {...renderProps} /> }) | ||
response.status(200).send(html) | ||
} else { | ||
response.status(404).send('Not found'); | ||
response.status(404).send("Not found") | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
export default universalReactAppMiddleware; | ||
export default universalReactAppMiddleware |
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