Created
February 28, 2020 15:53
-
-
Save ashour/d1dba8774c4d7c1fa73fbf44e6f5dd49 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React from "react"; | |
import express from "express"; | |
import { StaticRouter } from "react-router-dom"; | |
import { renderToString } from "react-dom/server"; | |
import App from "./common/App"; | |
import render from "./server/render"; | |
import { determineUserLang } from "./common/i18n"; | |
const server = express(); | |
server | |
.disable("x-powered-by") | |
.use(express.static(process.env.RAZZLE_PUBLIC_DIR)) | |
.get("/*", (req, res) => { | |
const context = {}; | |
const lang = determineUserLang(req.acceptsLanguages()); | |
const markup = renderToString( | |
<StaticRouter context={context} location={req.url}> | |
<App lang={lang} /> | |
</StaticRouter>, | |
); | |
if (context.url) { | |
res.redirect(context.url); | |
} else { | |
const html = render(markup); | |
res.status(200).send(html); | |
} | |
}); | |
export default server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment