Skip to content

Instantly share code, notes, and snippets.

@dagda1
Created February 18, 2020 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagda1/c2b5379256fd3e44fe1bb9903fd6e3e2 to your computer and use it in GitHub Desktop.
Save dagda1/c2b5379256fd3e44fe1bb9903fd6e3e2 to your computer and use it in GitHub Desktop.
export async function render({ req, res }: RendererOptions): Promise<void> {
const context: StaticRouterContext = {};
const appString = renderToString(
<StaticRouter location={req.url} context={context}>
<Routes />
</StaticRouter>,
);
const scriptTags = ['vendor', 'client']
.filter(k => !!assets[k]?.js)
.map(k => `<script src="${assets[k].js}" defer crossorigin></script>`)
.join('\n');
res.status(HttpStatusCode.Ok).send(`
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
${`<link rel="stylesheet" href="${assets.client.css}">`}
</head>
<body>
<div id="root">${appString}</div>
${scriptTags}
</body>
</html>
`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment