Skip to content
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

styled-components example not updated #28

Closed
AxelBriche opened this issue Oct 13, 2022 · 10 comments
Closed

styled-components example not updated #28

AxelBriche opened this issue Oct 13, 2022 · 10 comments
Labels
needs-response We need a response from the original author about this issue/PR

Comments

@AxelBriche
Copy link

I start a new project with npx create-remix@latest command and my entry.server.tsx file has 111 lines of code.
Styled-components entry.server.tsx file has only 29 line of code.

Can you update that example?

The exemple code use return new Response("<!DOCTYPE html>" + markup, {
But last remix version use new PassThrough() has new body value:

          const body = new PassThrough();

          responseHeaders.set("Content-Type", "text/html");

          resolve(
            new Response(body, {
              headers: responseHeaders,
              status: didError ? 500 : responseStatusCode,
            })
          );
@machour
Copy link
Collaborator

machour commented Oct 13, 2022

A pull request updating the example would be more than welcome.
Would be more than glad to help if you have some spare time to take care of it!

@kiliman
Copy link
Collaborator

kiliman commented Oct 13, 2022

I believe the new entry.server file supports React 18 streamining, whereas most of the older examples use React 17 and renderToString.

I'm not sure how simple, if even possible, it is to update the styled-components example to the streaming API.

As you can see, styled components first renders the markup as a string, while collecting styles, then injects those styles into the markup. With streaming, the HTML is already being sent to the client while rendering, so nowhere to inject the styles.

let markup = renderToString(
sheet.collectStyles(
<RemixServer context={remixContext} url={request.url} />
)
);
const styles = sheet.getStyleTags();
markup = markup.replace("__STYLES__", styles);

@MichaelDeBoey
Copy link
Member

@AxelBriche I'm working on a PR to update all examples to be in line with our templates.

@kiliman styled-components has an issue about HTML streaming: styled-components/styled-components#3658

@MichaelDeBoey MichaelDeBoey self-assigned this Oct 20, 2022
@joglr
Copy link

joglr commented Oct 20, 2022

@AxelBriche I'm working on a PR to update all examples to be in line with our templates.

@kiliman styled-components has an issue about HTML streaming: styled-components/styled-components#3658

It seems they have documented how to implement a working example with streaming rendering:

https://styled-components.com/docs/advanced#streaming-rendering

However, it uses the deprecated renderToNodeStream, not sure how to use it with renderToPipeableStream

This does not also not work with a streamed response though.

@MichaelDeBoey MichaelDeBoey removed their assignment Nov 23, 2022
@iDVB
Copy link

iDVB commented Jan 11, 2023

Any motion on this? We're wondering the exact same thing.
I guess the fallback is to use React 18 with renderToString instead?

@github-actions github-actions bot added the needs-response We need a response from the original author about this issue/PR label Mar 12, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Apr 2, 2023

This issue has been automatically closed because we haven't received a response from the original author 🙈. This automation helps keep the issue tracker clean from issues that are unactionable. Please reach out if you have more information for us! 🙂

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 2, 2023
@joglr
Copy link

joglr commented Apr 5, 2023

I'm migrating away from styled-components, so I can use a library that supports react 18 streaming rendering 🤷‍♂️

@freshollie
Copy link

freshollie commented Apr 19, 2023

For those still using the example styled-components. This can be used with the react 18 streaming example:

    const styleSheet = new ServerStyleSheet();

    // SSR render the full App
    const { pipe, abort } = renderToPipeableStream(
      styleSheet.collectStyles(
        <RemixServer
          context={remixContext}
          url={request.url}
          abortDelay={ABORT_DELAY}
        />
      ),
      {
        // use onShellReady to wait until a suspense boundary is triggered
        onShellReady() {
          responseHeaders.set("Content-Type", "text/html");
          const body = new PassThrough({
            transform: (chunk, _, done) => {
              // perform previous behaviour and replace the "__STYLES__" placeholder as it's streamed to the client
              const stringChunk = (chunk as Buffer).toString();
              done(
                undefined,
                Buffer.from(
                  stringChunk.replace("__STYLES__", styleSheet.getStyleTags())
                )
              );
            },
          });
          pipe(body);
          resolve(
            new Response(body, {
              status: didError ? 500 : responseStatusCode,
              headers: responseHeaders,
            })
          );
        },
        onShellError(err: unknown) {
          reject(err);
        },
        onError(err: unknown) {
          didError = true;
          console.error(err);
        },
      }
    );

Seems to be working so far for me!

@MichaelDeBoey
Copy link
Member

@freshollie We would love to get the styled-components example updated to work with React 18

@beeant
Copy link

beeant commented Jun 16, 2023

The example that @freshollie gave works, but why did he receive a thumbs down? Is there something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-response We need a response from the original author about this issue/PR
Projects
None yet
Development

No branches or pull requests

8 participants