title: Sending React Component styled with MUI5 as HTML string from client to server. Emotion cache data gets lost
body: <p>I'm sending a React Component styled with MUI5 as HTML string to the backend to generate a PDF. We have separate repos for React app and Node server. This official MUI5 <a href="https://mui.com/material-ui/guides/server-rendering/" rel="nofollow noreferrer">Server Rendering documentation</a> is the reference I settled on.</p>
  <p>Approach:</p>
  <ol>
  <li>Convert the component to HTML string, providing redux data as well as emotion cache and other MUI-related style data. Send HTML string and cache object to backend.</li>
  </ol>
  <pre><code>export const convertJsxToHtmlCache = (componentWithProps) =&gt; {
      const cache = createEmotionCache();
      console.log('cache: ', cache);
  
      const html = ReactDOMServer.renderToString(
          &lt;Provider store={store}&gt;
              &lt;CacheProvider value={cache}&gt;
                  &lt;ThemeProvider theme={theme}&gt;
                      &lt;CssBaseline /&gt;
                      {componentWithProps}
                  &lt;/ThemeProvider&gt;
              &lt;/CacheProvider&gt;
          &lt;/Provider&gt;,
      );
  
      return { html, cache };
  };
  </code></pre>
  <ol start="2">
  <li>Extract CSS from emotion cache and return a new HTML string that includes the extracted emotion CSS</li>
  </ol>
  <pre><code>export const renderFullPage = ({ html, cache }) =&gt; {
      console.log('CACHE: ', cache);
      const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(cache);
      const emotionChunks = extractCriticalToChunks(html);
      const emotionCss = constructStyleTagsFromChunks(emotionChunks);
      console.log('emotionCss: ', emotionCss);
  
      return `
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
        &lt;head&gt;
          &lt;title&gt;My page&lt;/title&gt;
          ${emotionCss}
          &lt;meta name=&quot;viewport&quot; content=&quot;initial-scale=1, width=device-width&quot; /&gt;
          &lt;link rel=&quot;stylesheet&quot; href=&quot;https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&amp;display=swap&quot; /&gt;
        &lt;/head&gt;
        &lt;body&gt;
          &lt;div id=&quot;root&quot;&gt;
                      ${html}
                  &lt;/div&gt;
        &lt;/body&gt;
      &lt;/html&gt;
    `;
  };
  </code></pre>
  <p>In step 2, an error is thrown that Emotion CSS is empty. It seems to be there in the Emotion cache logged in browser console, but not in the backend after transmitted as POST request body. If this is the issue, is there a way to transmit style information elements from app to server?</p>
  <p>Any advice / input would be appreciated.</p>