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

Styled-components SSR not working with new SC version

Open felisarmis opened this issue 4 years ago • 4 comments

Hello! I'm trying to follow this instruction https://reactjs.net/features/css-in-js.html#styled-components for styled-components SSR. But it works only for styled-components v4.0.0. If I try use the latest version - build failed with "window is undefined" error after import { ServerStyleSheet } from 'styled-components';. Please update SC SSR support.

felisarmis avatar Dec 10 '20 02:12 felisarmis

Sorry, I don’t have time to investigate a fix for this. It’s likely a problem with SC itself or the webpack configuration. Stick with v4 if you need support.

On Wed, Dec 9, 2020 at 18:54, Felis Armis [email protected] wrote:

Hello! I'm trying to follow this instruction https://reactjs.net/features/css-in-js.html#styled-components for styled-components SSR. But it works only for styled-components v4.0.0. If I try use the latest version - build failed with "window is undefined" error after import { ServerStyleSheet } from 'styled-components';. Please update SC SSR support.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/reactjs/React.NET/issues/1232, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHGCFQMT3YL6YUH6EYAA73SUAZ5DANCNFSM4UUMEHAA .

dustinsoftware avatar Dec 10 '20 14:12 dustinsoftware

@dustinsoftware i've tracked this issue down to these lines from styled-components:

https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/base.js#L41-L55

Using webpack.DefinePlugin to set process.env.NODE_ENV to "production" fixes the issue as the minifier removes those lines with 'window', and server rendered styles works without changing the StyledComponentsRenderFunction.

Obviously setting NODE_ENV like this isn't an ideal solution as we wouldn't want to set it in development.

I've created an issue in styled-components with a possible fix.

From my own tests, it looks like this issue was introduced in styled-components@>=5.2. Downgrading to 5.1 does not encounter this issue as there is a typeof window !== "undefined" check

mattywong avatar Jan 13 '21 07:01 mattywong

I can confirm - we are using version 5.1.1 without problems

vaclavholusa-LTD avatar Jan 17 '21 22:01 vaclavholusa-LTD

@dustinsoftware looks like we might be able to close this and #1207. Styled-components released an update couple months back with styled-components 5.3.1 (https://github.com/styled-components/styled-components/issues/3506) that should address this. I haven't tested it myself however (we have projects still on 5.1.x), but thought I'd give this issue a bump.

mattywong avatar Oct 19 '21 02:10 mattywong