Missing client side styles when using getStyleElement with SSR
See original GitHub issuestyled-components: 2.4.0 babel-plugin-styled-components: 1.3.0 NextJS: 4.2.1
Apologies in advance if this is an obvious issue- I got no response on Spectrum.
I have NextJS setup as instructed in the styled components/NextJS docs. The page renders with all required styles for all visible elements, but any “hidden” elements (ie. a modal that doesn’t show until user clicks, styled text hidden in a drawer, etc) has a SC class name generated for it, but no associated styles.
If I switch getStyleElement() for getStyleTags(), the missing styles are included in the server rendered styles, though any styles included via injectGlobal`` are lost. Am I missing something totally obvious?
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Server-side rendered styled-components with Nextjs
js comes configured with styled-jsx . But what if you want to use something else for styling? I've recently worked on setting up...
Read more >SSR with styled-components - when app.js loads site missing ...
I bet it's probably an issue with styled-components . The SSR works fine and everything else, except that one component which is missing...
Read more >Server Side Rendering - Loadable Components
Add loadableReady client-side. Loadable components loads all your scripts asynchronously to ensure optimal performances. All scripts are loaded in parallel, ...
Read more >Static Site Generation and Server-Side Rendering Using Next.js
When server-side rendering (SSR) is used, the server renders real HTML for every ... For styles, we want to use styled-components , install...
Read more >Advanced Usage - styled-components
Theming, refs, Security, Existing CSS, Tagged Template Literals, Server-Side Rendering and Style Objects.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@gribnoysup What’s the max internet kudos I can offer? This fixed my issue 🙏. I really appreciate it, and all the help from @philpl.
I’ll close the issue, but I’m also curious if you have a few more minutes- what led you to this solution in the first place? I don’t have a ton of experience with node module resolution intricacies so I’d love any tips on debugging these kinds of issues in the future.
@mhriess glad I could help! 😸
I think when I encountered this one of the first things I did was the same one that you tried: I just moved components from
shared
to theapp
and it just started working. This made me think that there was something wrong with the build setup because the code wasn’t changed, only the file position.I used
webpack-bundle-analyzer
to look at the bundle structure and noticed that thestyled-components
library is included twice in the bundle. You can try this yourself with the example repo I provided. If you addwebpack-bundle-analyzer
plugin to the webpack config, you will see something like this:Notice how
styled-components
is imported twice. You also can see that the second module is coming from theshared/node_modules
.To figure out how webpack resolves modules I just went to webpack docs. Thankfully it was already at the time when webpack 2.0 docs were live. They are structured well, search works great, and overall they are written pretty well.
We are interested in the module resolution process, more specifically in Module paths resolution part for now. We are not overriding anything in the config from the start, so we want to know what are the “reasonable defaults” for
resolve.modules
property that is mentioned in this part of the article.And here is our answer:
and