Skip to content

feat: styled-components & streaming #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

wetteyve
Copy link

@wetteyve wetteyve commented May 16, 2025

Introduces:

  • upgrade to remix v2
  • upgrade to react v19
  • upgrade to styled-components v6
  • switch to the vite plugin
  • switch to renderToPipeableStream instead of renderToString
  • should solve Vite + Styled Components Example #375

Streaming & Styled Components

Hello Remix community,

We've been working extensively with Remix at our company for about a year now. However, our legacy stack still requires (and likely will continue to require) support for styled-components.

Over the past few days, I’ve been working on enabling all the v3 future flags in our project to prepare for the transition to React Router v7. One challenge I ran into is that the v3_singleFetch flag explicitly requires switching from renderToString to renderToPipeableStream.

With the help of this comment, I arrived at a solution that I’d like to share and propose here. Unfortunately, using remix-island wasn't a viable option for us—we encountered issues with error boundaries, Remix's meta API, and complications with our CDN setup that made integration difficult.

With this implementation, everything appears to be working smoothly on our side. I have no prior experience with Node.js streaming, so I’m very happy to receive any feedback or suggestions you might have.

Thanks!

Open topics

  • I managed to break HMR in this example with my implementation. I tried to stick with the unnamed _boundary route as described in the README, but I couldn't figure out what exactly needs to be changed. Maybe someone knows how to prevent the app from unmounting on every change when using styled-components in watch mode.

@wetteyve wetteyve marked this pull request as ready for review May 16, 2025 22:41
Copy link

@arcastro arcastro left a comment

Choose a reason for hiding this comment

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

Left a single comment.
(I'm just a passerby who is also interested in using styled-components with remix, ty!)

Comment on lines +88 to +94
chunk instanceof Uint8Array
? decoder.decode(chunk, { stream: true })
: chunk.toString(encoding || "utf8");
renderedHtml = renderedHtml.replace(
"__STYLES__",
styleSheet.getStyleTags()
);

Choose a reason for hiding this comment

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

Isn't it possible for the __STYLES__ string to be truncated across two different chunks?
In that case, the styles would not get replaced.

Copy link
Author

Choose a reason for hiding this comment

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

in theory this could be true, you're right. unfortunately i don't understand node streaming and how the chunks are cut in depth. however, we have been using this approach in production for 2 weeks (first with remix v2 and now with react-router v7) and have never noticed this side effect.

Copy link

@arcastro arcastro May 28, 2025

Choose a reason for hiding this comment

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

In my own project, I ended up using

Which handles chunk boundaries correctly, but I'd lean against including that additional dependency in this pull request.

I do think the edge case should be handled correctly, but I don't have any alternative suggestions as to how, other than reimplementing similar logic. But that seems like overkill maybe 🤷‍♂️

@wetteyve wetteyve marked this pull request as draft May 28, 2025 09:39
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.

2 participants