Last active
October 19, 2018 16:09
-
-
Save jkinkead/d393e56de3d8fa5a496ac45ee7ae2ecd to your computer and use it in GitHub Desktop.
Cloud Function to serve prerendered React
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 fs from "fs"; | |
import path from "path"; | |
import React from "react"; | |
import ReactDOMServer from "react-dom/server"; | |
import App from "./app"; | |
const htmlData = ` | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Serverlessly Rendered</title> | |
</head> | |
<body> | |
<div class="container"> | |
<div id="container"></div> | |
</div> | |
</body> | |
</html> | |
` | |
export const render = (req, res) => { | |
const html = ReactDOMServer.renderToString(<App />); | |
return res.send( | |
htmlData.replace('<div id="container"></div>', `<div id="container">${html}</div>`) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment