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.

CSS grows during successive server-side rendering

See original GitHub issue

In our SSR we see an increase in CSS response size whenever a new route / React-loadable page component has been triggered.

For example the CSS sizes would grow as follows:

  • Homepage: 70 kB
  • Product detail page: 80 kB
  • Homepage: 75 kB

Reproduction

I don’t have a separate reproduction, and saw that there is already a test case for SSR that checks that styles don’t leak. But maybe we’re triggering some special path (e.g. Global Styles?) that are not covered by the test?

Steps to reproduce

In our app:


    const styleSheet = new ServerStyleSheet();

      renderToString(
        styleSheet.collectStyles(
          <>
            <GlobalStyle />
            <HelmetProvider context={context}>
              <Capture report={moduleName => modules.push(moduleName)}>
                {children}
              </Capture>
            </HelmetProvider>
          </>
        )

console.error(styleSheet.getStyleTags().length); // this grows after a different page was rendered

Expected Behavior

When loading the same page / route, the CSS size should be the same from the first to the last request.

Actual Behavior

CSS size grows over time when switching between different pages / routes.

Workaround

I found the follow workaround: Resetting the master StyleSheet (no idea why we’d need that anyway, as it’s empty for the first render, so I thought it wouldn’t hurt to be also empty on successive renders. But maybe it helps with caching / performance?):

const StyleSheet = require('styled-components')
  .__DO_NOT_USE_OR_YOU_WILL_BE_HAUNTED_BY_SPOOKY_GHOSTS.StyleSheet;


    StyleSheet.reset(true); // ⬅️ the fix
    const styleSheet = new ServerStyleSheet();

Might this be a repetition / related to #1602?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
tpcommented, Jun 17, 2019

@probablyup Sorry for the late reply, we’ve been busy shipping other updates.

Just checked with alpha (954ab995b8b3a5efed5403249c1014aa3432effb), and the behavior is indeed good (no size variations across requests).

3reactions
probablyupcommented, Jun 17, 2019

Awesome, great to hear! There’s a bunch of fixes in the beta channel so if you ship that at work I recommend using styled-components@beta now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is server-side rendering and how does it improve site ...
Server-side rendering ensures that website content appears quickly, without first having to download and run application code.
Read more >
Components: Server-Side vs. Client-Side | CSS-Tricks
Server components can take two flavors. The first is the classic approach, which is to render components in real-time based on a request ......
Read more >
Client-side Vs. Server-side Rendering: What to choose when?
Hence, server-side rendering increases the page load time compared to the page load time for a static site where there is no dynamic...
Read more >
Server Side Rendering (SSR) vs. Client Side Rendering (CSR ...
Client Side Rendering means generating the HTML components on the browser side, by executing Javascript code within the browser that manipulates the HTML ......
Read more >
Client Side Rendering vs. Server Side Rendering vs. Static ...
Unlike Client-Side Rendering, every subsequent time the user takes action to visit a different page, the rendering process repeats.
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