Our React app uses universal rendering. The html/body/head elements of the page are generated server-side using a React component. Normally, we can create things like <script>
and <link>
elements in our <head>
just fine via JSX. Unfortunately, things got tricky once we started needing to insert third-party HTML fragments. These fragments are essentially a string like '<script src="some/url.js"></script><link rel="stylesheet" href="/some/css">'
. Normally, HTML fragments can be injected into some element container via dangerouslySetInnerHTML
, but there are no container elements for the <head>
of an HTML document. This has forced us to either:
- Parse the HTML fragments (no easy task) and convert them into React elements
- Abandon JSX and build all our head content as one big concatenated/templated string
Is there any way in React to inject an HTML fragment into a document without a container element?