Skip to content

Instantly share code, notes, and snippets.

@jkinkead
Last active October 19, 2018 16:09
Show Gist options
  • Save jkinkead/d393e56de3d8fa5a496ac45ee7ae2ecd to your computer and use it in GitHub Desktop.
Save jkinkead/d393e56de3d8fa5a496ac45ee7ae2ecd to your computer and use it in GitHub Desktop.
Cloud Function to serve prerendered React
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