question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

renderToString() doesn't embed styles when called in the browser

See original GitHub issue

The problem

When calling React’s renderToString() in the browser, emotion won’t embed the styles into the output HTML. If I’m not mistaken, this was discussed before in https://github.com/emotion-js/emotion/issues/1245, but in the context of jest and without a solution that would work in a real browser.

Proposed solution

I suspect that my problem would be solved by removing the isBrowser check in here: https://github.com/emotion-js/emotion/blob/f4b9dc6ea6863e499586a61b9fd64f2c2cdf4092/packages/styled/src/base.js#L135 I’m pretty sure though it’s there for a reason that’s beyond my knowledge of how emotion is implemented. 😃 I’d appreciate some input on what’s behind that check.

Alternative solutions

Me giving up my plans and rendering on the server instead 🤷‍♀️

Additional context

The reason why I’m trying to do this in the first place is that I’m looking for a way to render React code as HTML that I can in turn feed into an HTML-to-PDF converter, preferably all in the browser.

Using emotion for this might be a bit of a stretch, but I’m close enough to make it happen that I feel like there must be some way.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Nov 16, 2020

The easiest that comes to my mind is to replace <Global/> with an empty element that renders :global() styles after adding a namespace plugin: https://codesandbox.io/s/long-star-x8zmy?file=/src/App.js

1reaction
Andaristcommented, Nov 13, 2020

Using @emotion/react this can be quite trivial to achieve: https://codesandbox.io/s/interesting-nash-ipyko?file=/src/App.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

No loading/serving of stylesheets - react - Stack Overflow
It doesn't embed CSS but only exports the identifier mappings. This also explains why you're not able to access the style variable ....
Read more >
ReactDOMServer – React
The following methods can be used in the environments that don't support streams: renderToString(); renderToStaticMarkup(). Reference. renderToPipeableStream().
Read more >
Tag: renderToString - somewhat abstract
When rendering in a browser, our React application is mounted so that it will dynamically update based on events like mouse movements, ...
Read more >
Server Side Rendering - Emotion
This returns a string of html that inlines the critical css required right before it's used. import { renderToString } from 'react-dom/server' import ......
Read more >
Rendering on the Web
CSR: Client-Side Rendering - rendering an app in a browser, ... React users can use renderToString() or solutions built atop it like Next.js ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found