Skip to content

WIP: Fix: SSR doesn't include styles - #3061

Closed
StevenLangbroek wants to merge 2 commits into
styled-components:mainfrom
StevenLangbroek:fix/hydration
Closed

StevenLangbroek wants to merge 2 commits into
styled-components:mainfrom
StevenLangbroek:fix/hydration

Conversation

@StevenLangbroek

@StevenLangbroek StevenLangbroek commented Mar 12, 2020 •

Copy link
Copy Markdown

Note: this is extremely early, I'm opening the PR to work out in the open and leave some documentation with regards to how to debug & fix these kind of issues.

Todo

Resolves #2962

The problem appears only in the built version of sc, if you want I can duplicate the ssr test suite but run it against built version as a separate step to prevent this in the future. Thoughts? /cc @kitten @probablyup

Result so far

I've narrowed the problem down to this:

If I run the tests as normal, the DefaultGroupedTag contains a normal amount of rules (found by logging tag in outputSheet). If I build sc, and point the ServerStyleSheet import at the built assets (as this branch does now), the rules array is empty however. My best guess is the bug is somewhere in insertRules on DefaultGroupedTag or insertRule on the underlying Tag.

Development:
image

Production:
image

@StevenLangbroek

StevenLangbroek commented Mar 12, 2020 •

Copy link
Copy Markdown
Author

I've eliminated some suspects in the reproduction:

  • React Router
  • Razzle's HMR setup

Since the tests in ssr.test.js pass and include styles, a potential suspect might be differences between the development & distributed code, will investigate that first.

@StevenLangbroek

StevenLangbroek commented Mar 12, 2020 •

Copy link
Copy Markdown
Author

Test output in reproduction repo:

    <style data-styled="true" data-styled-version="5.0.1"></style>
    <div class="Home__Wrapper-okpba1-0 ivJaOt">
        <div>
            <img src="react.svg" alt="logo" />
            <h2>Welcome to Razzle</h2>
        </div>
        <p>To get started, edit <code>src/App.js</code> or
            <!-- --> <code>src/Home.js</code> and save to reload.</p>
        <ul>
            <li><a href="https://github.com/jaredpalmer/razzle" class="Home__Anchor-okpba1-1 bsrTZk">Docs</a>
            </li>
            <li><a href="https://github.com/jaredpalmer/razzle/issues" class="Home__Anchor-okpba1-1 bsrTZk">Issues</a>
            </li>
            <li><a href="https://palmer.chat" class="Home__Anchor-okpba1-1 bsrTZk">Community Slack</a>
            </li>
        </ul>
    </div>

Test code:

import React from "react";
import { renderToNodeStream } from "react-dom/server";
import { ServerStyleSheet } from "styled-components";
import ofStream from "stream-to-promise";
import Home from "./Home";
import beautify from "beautify";

describe("Style Hydration", () => {
  it("should include styles", () => {
    const sheet = new ServerStyleSheet();
    const render$ = renderToNodeStream(<Home />);
    const style$ = sheet.interleaveWithNodeStream(render$);

    return ofStream(style$)
      .then(b => b.toString())
      .then(received => {
        console.log(beautify(received, { format: "html" }));
      });
  });
});

Reference: https://github.com/StevenLangbroek/sc-v5-streaming-repro/blob/master/src/server.spec.js

Run tests with yarn razzle test btw, not yarn test

@StevenLangbroek

StevenLangbroek commented Mar 12, 2020 •

Copy link
Copy Markdown
Author

Ok, that was a good idea (I am A Very Smart Boy please pat my head thank you).

So, I went back into packages/styled-components, ran yarn build, and changed the import in ssr.test.js to refernece styled-components.esm.js from dist instead of from src, and lo and behold:

image

@StevenLangbroek

Copy link
Copy Markdown
Author

Tried cjs and vanilla styled-components.js as well, they all exhibit the same behavior.

@StevenLangbroek

Copy link
Copy Markdown
Author

Ruled out terser

@StevenLangbroek

Copy link
Copy Markdown
Author

Adding console.log(sheet.interleaveWithNodeStream.toString()) yields:

image

So it seems output is being dropped by rollup...

@StevenLangbroek

Copy link
Copy Markdown
Author

So _emitSheet's output is an empty style tag. I took a look and added console statements to outputSheet, which leads me to this suspect output:

image

I'll keep digging unless anyone can point me in a specific direction for a potential culprit.

@quantizor

quantizor commented Mar 12, 2020 via email

Copy link
Copy Markdown
Contributor

@StevenLangbroek

Copy link
Copy Markdown
Author

This is all in the styled-components repo itself...

@quantizor

Copy link
Copy Markdown
Contributor

Gotcha, sorry continue! It might have to do with the SERVER replacement token in our rollup config. Though I’d think the cjs and esm versions would work the same

@StevenLangbroek

Copy link
Copy Markdown
Author

Yeah I tried all bundles, behavior is the same everywhere. Digging through the codebase now to remove it everywhere and see if that fixes things...

@StevenLangbroek

Copy link
Copy Markdown
Author

Removing the __SERVER__ flag so the interleaveWithNodeStream function is complete did not help unfortunately btw.

@StevenLangbroek

Copy link
Copy Markdown
Author

In the built version, there are 0 rules, compared to 2 when running against dev:

image

(see #3061 (comment))

@StevenLangbroek

Copy link
Copy Markdown
Author

Ok, I'm now in DefaultGroupedTag internals, and could really use some guidance regarding its architecture if anybody has time.

@StevenLangbroek
StevenLangbroek requested a review from kitten March 12, 2020 20:40
@kitten

kitten commented Mar 12, 2020

Copy link
Copy Markdown
Member

Alright; let’s jump in this tomorrow and I’ll take a look in the meantime and will verify that it’s not just a small mistake somewhere 😅

@StevenLangbroek

StevenLangbroek commented Mar 12, 2020 •

Copy link
Copy Markdown
Author

Thanks @kitten 🙇. Also interesting that output isn't complete (closing tags missing), but maybe that's for another time #3061 (comment) nvm

@quantizor

Copy link
Copy Markdown
Contributor

Any movement on this guys?

@StevenLangbroek

Copy link
Copy Markdown
Author

holy shit I had lost track of this entirely (sorry, zombie apocalypse etc). I'll pick this back up tomorrow. @kitten gave me a good walkthrough of the serverstylesheet and the gnarly array-like stuff going on there, I'm gonna dig into it further.

@DonovanCharpin

Copy link
Copy Markdown

I see the same issue when building with V5.x.x, When loading the page, the css is not entirely present and then is hydrated. I think that could be related to this PR. I'm using Nextjs 9.3.4. It's really visible so I'm going to publish a version if it can help.

@StevenLangbroek

Copy link
Copy Markdown
Author

This is WIP, I'm gonna pick it up again as soon as I can.

@tim-soft

Copy link
Copy Markdown

@StevenLangbroek Is there anything I can help with and have you had any epiphanies on this? From what I've read in here so far, it sounds like you've narrowed it down to a build issue?

@StevenLangbroek

Copy link
Copy Markdown
Author

@tim-soft wow sorry, I really dropped the ball on this one. I had time off to spend digging into it, but am employed again so it kind of fell by the wayside. @kitten gave me an intro to how the Uint8Arrays work and the ID assignment, but I wasn't really closer to finding the root cause. There's a failing test and a repro, if you want to take a look that'd be great. I'll be back at it over the weekend, if there's anything I can do to help please ping me.

import stylisRTLPlugin from 'stylis-plugin-rtl';
import ServerStyleSheet from '../models/ServerStyleSheet';
import ofStream from 'stream-to-promise';
import { ServerStyleSheet } from '../../dist/styled-components.esm';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m actually not sure anymore whether this is a valid reproduction. But it could also hint at the root cause.

This bundle includes the global GroupIDAllocator, but the styled function that’s used in this test is imported separately by Jest:

import { resetGroupIds } from '../sheet/GroupIDAllocator';

So none of the group IDs are carried over and will match 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense. I'm still curious why my repro behaves the way it does then...

@StevenLangbroek

Copy link
Copy Markdown
Author

@tim-soft ok, I'm not able to continue work on this :(. If you want we can jump on a call and I can walk you through how we noticed this bug, what I did to investigate and the current status.

@StevenLangbroek

Copy link
Copy Markdown
Author

@tim-soft @kitten what can we do about this? my current job doesn't need SSR and as you know I'm a little busy. Can I do anything to hand this over and help move it forward?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Collecting style sheet on server broke after upgrading to v5.0.0

5 participants