Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created September 6, 2018 17:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kentcdodds/e1145b4c662362ec49c0ff871ab974ea to your computer and use it in GitHub Desktop.
Save kentcdodds/e1145b4c662362ec49c0ff871ab974ea to your computer and use it in GitHub Desktop.
/**
* In our app, we have a few middleware that generate a string of HTML.
* On occassion it's fine to just use dangerouslySetInnerHTML directly for
* those, but that requires that you have a host node for the innerHTML.
*
* In certain scenarios (like tags in <head />), there's HTML that we need
* to insert directly where it is. This component enables that because
* we replace <raw-text> and </raw-text> with empty strings. Effectively
* making whatever's between <raw-text> and </raw-text> inlined in place.
*
* This is not something you should typically use, but it can be useful
* on occassion.
*/
function RawText({ children }) {
return <raw-text dangerouslySetInnerHTML={{ __html: children }} />
}
function App(props) {
return (
<html>
<head>
<RawText>{props.marketingAssets.headHTML}</RawText>
</head>
<body>{`... etc...`}</body>
</html>
)
}
function renderApp(props) {
return `
<!DOCTYPE html>
${ReactDOMServer.renderToStaticMarkup(<App {...props} />).replace(
/<\/?raw-text>/g,
'',
)}
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment