React.NET icon indicating copy to clipboard operation
React.NET copied to clipboard

Global Styles in styled-components broken in sc 5.x

Open johannesaxner-valtech opened this issue 5 years ago • 4 comments

In short, everything works fine with styled-components version 4.x, but when we tried upgrading to version 5.x, global styles would not be injected for server-side rendering, only component styles. The symptom is flash of unstyled content until react has loaded and started, in our very visible due to us reseting margins etc in our styled-components global styles.

I'm a front-end developer so not sure about all environment details but the only thing changed between working and not working is bumping the version of styled-components from 4.4.1 to 5.2.0.

johannesaxner-valtech avatar Sep 18 '20 09:09 johannesaxner-valtech

We've got same problem(

@johannesaxner-valtech Did you solve it somehow?

Iminrage avatar Dec 09 '20 16:12 Iminrage

No, we've just stayed with styled-components 4.

johannesaxner-valtech avatar Dec 16 '20 08:12 johannesaxner-valtech

Looks like this is a 5.2 issue, introduced from this change:

https://github.com/styled-components/styled-components/commit/f12b943658a9731ab6af6548a780019f696a09cb#diff-da81e98b76a2ff1450584f4cc5c815939012d7d1e067e90b759be54ddf058928L52-R54

downgrading to 5.1.x should resolve this.

Looks like that __SERVER__ boolean variable is set at compile time to create a browser and non-browser dist bundle?. This is sort of related to https://github.com/reactjs/React.NET/issues/1232. Because of this, the useLayoutEffect lines are the one that gets bundled (https://github.com/styled-components/styled-components/blob/f12b943658a9731ab6af6548a780019f696a09cb/packages/styled-components/src/constructors/createGlobalStyle.js#L60-L63).

@dustinsoftware would you have any ideas about the above? I'm thinking perhaps a separate webpack config for server and browser, where server webpack config would have targets: "node" set, but when I tried that the JSEngine isn't able to resolve the modules (complaining about 'export' and 'import' syntax from memory).

I'm happy to create an issue in styled-components repo about this, though I do not understand fully the context of why they have changed this check from typeof window !== "undefined" to __SERVER__ in this commit (https://github.com/styled-components/styled-components/commit/f12b943658a9731ab6af6548a780019f696a09cb)

mattywong avatar Jan 13 '21 09:01 mattywong

@mattywong did you find a way to solve this issue. Have the same issue. Just got this project to work on and not sure if sticking to 5.1.1 breaks any-other potion of the site. And __SERVER__ is hard-coded in rollup.config.js.

Thanks

nuwan-dammika avatar May 17 '21 05:05 nuwan-dammika