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

Suspensed parts of page and moving styles to top #69

Open
KoderFPV opened this issue Oct 15, 2024 · 4 comments
Open

Suspensed parts of page and moving styles to top #69

KoderFPV opened this issue Oct 15, 2024 · 4 comments

Comments

@KoderFPV
Copy link

Hi,

We want to introduce streaming rendering and suspended components to our application.
And we cannot handle the hydration process well.

The library's documentation mentions the strategy of moving styles to head just before hydration. However in the case of suspended/streamed components, we are already in the hydration process, so how we can move those styles if even its possible?

Thanks

@theKashey
Copy link
Owner

There could be an option to inject a little piece of script after every injected style to move it to the top, but it likely will only cause extra re-renders and make things slow.

AFAIK streaming response should still wait for the stream to end before starting hydration as "opened tags should be closed".
If you are talking about React 18's "late hydration", then it's not based on pure HTML this library can handle.

@KoderFPV
Copy link
Author

KoderFPV commented Oct 16, 2024

> There could be an option to inject a little piece of script after every injected style to move it to the top, but it likely will only cause extra re-renders and make things slow.

That might be the only option. I want to try. Fortunately, still, some of the styles will still be moved to the head from non-streamed/non-suspended part of the application before hydration.
@theKashey could you guide me a little bit, how can I do it? Do you think should I modify the createCriticalStyleStream function inside of the library, or can I modify the stream somehow during rendering? At the beginning, I would like to add after any injected <style>, single tag <script> with any JS, later I will put correct code to move styles.

AFAIK streaming response should still wait for the stream to end before starting hydration as "opened tags should be closed".
If you are talking about React 18's "late hydration", then it's not based on pure HTML this library can handle.

I mean components inside with async logic inside. We looking for INP (interaction to next paint) improvements. We wanna push the header, footer, and some other parts of the page, to be interactive (and move styles before hydration) and not be blocked for the heavier part of the app rendered later, after hydration.

@KoderFPV
Copy link
Author

KoderFPV commented Nov 17, 2024

The solution was simpler than we though.
Each streamed component inside suspense has its hydration process, exactly as explained in the documentation for the whole application.

So, each streamed component, outside of lifecycle (means ever render/re-render) checks if any styles to move exist, if so, it moves them to head. If don't, do nothing.

We still developing streaming rendering so it's not tested on production yet, but so far so good ;)

beforeReactHydration launches after the component finally renders on the client.
image
image

@theKashey
Copy link
Owner

Wondering what calls the code that calls beforeReactHydration.

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

No branches or pull requests

2 participants