Skip to content

Server side rendering breaks if you after styleSheet.reset() #378

Closed

Description

@ForbesLindesay

Version

v2 branch in github

Reproduction

import React from 'react';
import {renderToString} from 'react-dom/server';
import styled, {styleSheet} from 'styled-components';

const Heading = styled.h1`
  color: red;
`;

renderToString(<Heading>Hello World</Heading>);

console.log('run 1:')
console.dir(styleSheet.getCSS());

styleSheet.reset();

renderToString(<Heading>Hello World</Heading>);

console.log('run 2:');
console.dir(styleSheet.getCSS());

Outputs:

run 1:
'.styled-components-bug__Heading-ecpbxy {}\n.kDmCMo { color: red; }'
run 2:
''

Steps to reproduce

Attempt to render a component server side by resetting the style sheet, rendering it, then getting the css.

Expected Behavior

You should get the same css every time you render.

Actual Behavior

After resetting, you get empty css.

Activity

  1. mxstbr commented on Jan 14, 2017

    @mxstbr
    Member

    Thanks for testing the v2 branch, much appreciated!

    /cc @ThisGuyChris any ideas what's up here?

  2. teonik commented on Jan 14, 2017

    @teonik

    I can confirm this issue.Also happens on master if you call styleSheet.flush().

    I think this happens because components are imported once in the server so the ComponentStyle constructor, which calls inject() on the styleSheet, is also called once for every styled component.
    reset() clears all cssRules on the stylesheet and the components are left with dangling pointers to previous rule entries.

    I will look more into it when i got the time and provide some feedback.

  3. geelen commented on Jan 15, 2017

    @geelen
    Member

    Ah yep I put in a cache in ComponentStyle. That should be moved across to StyleSheet and cleared when you call reset. I'd do it but I'm on my phone :)

  4. modified the milestone: v2.0 on Jan 17, 2017
  5. self-assigned this
    on Jan 18, 2017
  6. added a commit that references this issue on Jan 18, 2017
    67f6889
  7. wmertens commented on Mar 9, 2017

    @wmertens
    Contributor

    in the mean time, you can do SSR by simply not calling reset() 😄 . It's a little less efficient byte-wise but not terrible.

  8. wmertens commented on Mar 30, 2017

    @wmertens
    Contributor

    My little workaround results in weird styles after a while :( I'd like to see this fixed before we go into production, soon.

    @geelen is fixing this really as easy as moving a cache var to a different place or does it mean updating the code all over the place?

  9. mxstbr commented on Apr 1, 2017

    @mxstbr
    Member

    This will be fixed with the new API I'm pretty sure (ref #386) since you won't need to call reset anymore.

  10. seidtgeist commented on Apr 14, 2017

    @seidtgeist

    Would you accept a fix for reset() in an interim 2.0.0-xx release until #386 has replaced the current API?

  11. mxstbr commented on Apr 14, 2017

    @mxstbr
    Member

    Sure!

  12. seidtgeist commented on Apr 15, 2017

    @seidtgeist

    @geelen Could you nudge me in the right direction here? I saw @ThisGuyChris’s commit but it breaks the styleSheet.reset() test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions