You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
difficulty: advancedIssues that are complex, e.g. large scoping for long-term maintainability.proposalThis issue is a proposal, usually non-trivial change
I have some experience with the setup patterns for emotion (incl. v11) and styled-components here, and would like to help with this. As far as I can tell from the source, this would require additions to the Lifecycle APIs so that the serverEntry.js and clientEntry.ts files' rendering functions could be modified. I would need some guidance on creating these hooks.
Anyone want to help me get started?
Reacted by Thibaut Tiberghien, Florian KΓΆrner, Ema Suriano, Rohit Gohri, Olivier Tassinari, Michal KrejΔΓ, Kevin Tang and Brennan Kinney
This is definitively something we should have, but is not so easy.
We also need to extract critical CSS to inline it in the pages.
It's already been mentioned somewhere that we may need to do something similar to how Gatsby works (docusaurus-browser/docusaurus-ssr files...).
I don't have much more infos to provide, this needs exploration :)
Maybe a good start would be to study all the css-in-js gatsby plugins, and maybe create some kind of doc/spreadsheet, to see what are the useful lifecycles / api surface to provide to make it possible to support a wide range of CSS-in-JS libraries.
Yes, off the top of my head the necessary operations for CSS-in-JS libs tend to be:
wrap the <App /> component with custom Providers which enable collection and critical extraction
set <head/> elements with those extracted styles, per page
extend the babel config, usually to provide a custom JSX function
...and in the case of exctraction libs like astroturf and linaria: extend the webpack config with custom loaders
The latter 3 are all supported in Docusaurus in one way or another AFAICT; what's really missing is wrapping the <App /> element.
In Gatsby as I'm sure you know, this is done through the wrapRootElement and/or wrapBodyElement functions, which are exported from gatsby-ssr.js and gatsby-browser.js.
It seems to me, that these correspond to the serverEntry.js and clientEntry.ts files I mentioned above... so I'm thinking some plugin APIs like wrapServerRootElement and wrapClientRootElement, as long as they could also provide a way to set Head elements, would do the trick
@slorber on closer examination, after trying to hack this today: I'm guessing when you said "it's not so easy" you might have been thinking about react-loadable...? This scheme doesn't seem so far to be compatible with the HOCs that are needed for collecting CSS from the component tree
@slorber yes functions for wrapping root and/or page element would also solve #2891. I see that you've indicated it as low-priority, but IMO it's rather important, e.g. for components or context-providers that may be maintaining global state and doing async operations, such as an analytics consent-manager which is causing me trouble at the moment
@slorber@lunelson Could you suggest any workaround to avoid a flash of unstyled content using docusaurus and styled-components (or any other CSS-in-JS lib as far as I understand)?
I would be happy to help implementing a real solution if you could suggest some direction.
An example of the problem I am facing with the code here (you might need to simulate a slower network to experience that)
I found a super hacky way to collect styles for styled-components, and I do not recommend it to anyone, but it works :). Most likely, it breaks hydration and forcing react to re-render the whole tree. Sadly, I don't know how to force the non-production build to check, but honestly, this is much better than FOUC.
I'm voting for public API, which will allow returning your element before passing it to renderToString from react-dom/server.
Hacky way
Create component for collecting styles from children:
// /src/theme/Root.jsimportReactfrom'react'importServerStylefrom'./ServerStyle'functionRoot({ children }){return(<><ServerStylefrom={children}/>{children}</>)}exportdefaultRoot
UPD: Fixed DocusaurusContext imports UPD (2022-05-06): No longer works for v2.0.0-beta.16+ UPD (2022-06-10): Fixed for v2.0.0-beta.21+ thanks to @EmaSuriano!
I'd be interested if someone could help design an API that works for most CSS-in-JS libs, and also allow composition (ie one plugin provides its CSS-in-JS integration, and you can use 2 CSS-in-JS libs by combining 2 plugins)
This requires studying the ecosystem a bit.
I'd like to have a table with expected CSS-in-JS libs compatibility before merging a PR.
I wanted to report having the same issue here w/ rendering any styled-components in my extended/swizzled Docusaurus templates. This caused a good deal of flicker and in some cases the CSS did not load initially at all. As of now, the workaround @exah describes above is working well for me when I render these components separately during the Layout rendering when the window is not yet available. Some Docusuarus components are SSR safe, but others which rely on Scroll Context or Announcement Context etc do not seem to be at the moment. Just sharing for anyone else facing this challenge.
The usage of React Server Components is growing, and we plan to adopt them in Docusaurus.
And we see that the past generation of runtime-based CSS-in-JS libs does not play with React Server Components (more explanations here: #8959).
At this point I'm not even sure we need to have these hooks anymore, it goes against the industry trend to adopt no-runtime solutions.
Many existing libs are in the process of figuring out how to benefit most from Server Components, so I think we should wait and decide later what is best for Docusaurus.
For the moment, I'm leaning towards not implementing these hooks because I believe it wouldn't encourage the usage of performant code by default in the long term, and the only good reason to implement those is to be compatible with legacy UI libs.
difficulty: advancedIssues that are complex, e.g. large scoping for long-term maintainability.proposalThis issue is a proposal, usually non-trivial change
Activity
[-]CSS-in-JS Rendering Hooks[/-][+][v2] CSS-in-JS Rendering Hooks[/+]slorber commentedon Aug 8, 2020
Hi,
This is definitively something we should have, but is not so easy.
We also need to extract critical CSS to inline it in the pages.
It's already been mentioned somewhere that we may need to do something similar to how Gatsby works (docusaurus-browser/docusaurus-ssr files...).
I don't have much more infos to provide, this needs exploration :)
Maybe a good start would be to study all the css-in-js gatsby plugins, and maybe create some kind of doc/spreadsheet, to see what are the useful lifecycles / api surface to provide to make it possible to support a wide range of CSS-in-JS libraries.
lunelson commentedon Aug 8, 2020
Yes, off the top of my head the necessary operations for CSS-in-JS libs tend to be:
<App />component with custom Providers which enable collection and critical extraction<head/>elements with those extracted styles, per pageastroturfandlinaria: extend the webpack config with custom loadersThe latter 3 are all supported in Docusaurus in one way or another AFAICT; what's really missing is wrapping the
<App />element.In Gatsby as I'm sure you know, this is done through the
wrapRootElementand/orwrapBodyElementfunctions, which are exported fromgatsby-ssr.jsandgatsby-browser.js.It seems to me, that these correspond to the
serverEntry.jsandclientEntry.tsfiles I mentioned above... so I'm thinking some plugin APIs likewrapServerRootElementandwrapClientRootElement, as long as they could also provide a way to setHeadelements, would do the tricklunelson commentedon Aug 8, 2020
Examples from Gatsbyβnote: many only require a babel plugin for the client side, and only modify rendering for the SSR part
lunelson commentedon Aug 10, 2020
@slorber on closer examination, after trying to hack this today: I'm guessing when you said "it's not so easy" you might have been thinking about
react-loadable...? This scheme doesn't seem so far to be compatible with the HOCs that are needed for collecting CSS from the component treeslorber commentedon Aug 11, 2020
Thanks for the details @lunelson
I don't have time to investigate these things right now but we'll likely come back to it once i18n is there
slorber commentedon Aug 17, 2020
Note, we might introduce a way to wrap root element in this PR: #3153
lunelson commentedon Oct 22, 2020
@slorber yes functions for wrapping root and/or page element would also solve #2891. I see that you've indicated it as low-priority, but IMO it's rather important, e.g. for components or context-providers that may be maintaining global state and doing async operations, such as an analytics consent-manager which is causing me trouble at the moment
slorber commentedon Oct 22, 2020
@lunelson in the meantime alpha66 declare a new component "LayoutProviders" that you can swizzle and wrap with custom providers.
For minimum maintenance burden, you can also check this doc to "enhance" an existing theme comp without duplicating its code: https://v2.docusaurus.io/docs/using-themes/#wrapping-theme-components
But I understand the issue about stateful providers resetting their states. We'll try to solve this soon after I finish i18n.
lunelson commentedon Oct 26, 2020
Thanks for the tips @slorber, good to know about those new components, but indeed yes the remounting-on-route-change problem remains open
sawasawasawa commentedon Jan 18, 2021
@slorber @lunelson Could you suggest any workaround to avoid a flash of unstyled content using docusaurus and
styled-components(or any other CSS-in-JS lib as far as I understand)?I would be happy to help implementing a real solution if you could suggest some direction.
An example of the problem I am facing with the code here (you might need to simulate a slower network to experience that)
8 remaining items
exah commentedon Mar 2, 2021
Hey guys,
I found a super hacky way to collect styles for
styled-components, and I do not recommend it to anyone, but it works :).Most likely, it breaks hydration and forcing react to re-render the whole tree. Sadly, I don't know how to force the non-production build to check, but honestly, this is much better than FOUC.I'm voting for public API, which will allow returning your element before passing it to
renderToStringfromreact-dom/server.Hacky way
children:Root:UPD: Fixed
DocusaurusContextimportsUPD (2022-05-06): No longer works for v2.0.0-beta.16+
UPD (2022-06-10): Fixed for v2.0.0-beta.21+ thanks to @EmaSuriano!
[-][v2] CSS-in-JS Rendering Hooks[/-][+]CSS-in-JS Rendering Hooks[/+]adamborowski commentedon Feb 4, 2022
I think that we can add a plugin point here:
https://github.com/facebook/docusaurus/blob/main/packages/docusaurus/src/client/serverEntry.tsx#L88
It basically renders whole app so we can add a plugin lifecycle method that can customize somehow this method,
and in case of styled components the plugin should do sth like this:
slorber commentedon Feb 9, 2022
Yes, that seems like a reasonable thing to do.
I'd be interested if someone could help design an API that works for most CSS-in-JS libs, and also allow composition (ie one plugin provides its CSS-in-JS integration, and you can use 2 CSS-in-JS libs by combining 2 plugins)
This requires studying the ecosystem a bit.
I'd like to have a table with expected CSS-in-JS libs compatibility before merging a PR.
This is the Gatsby API: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-ssr/#replaceRenderer
cieldon32 commentedon May 27, 2022
en...
I can use styled-components
I use "babel-plugin-styled-components",
and then, use in the part of render, it is work .
ntucker commentedon Aug 1, 2022
I would recommend considering https://github.com/callstack/linaria as it is much better for performance than others.
priley86 commentedon Oct 24, 2022
hello Docusaurus friends π
I wanted to report having the same issue here w/ rendering any styled-components in my extended/swizzled Docusaurus templates. This caused a good deal of flicker and in some cases the CSS did not load initially at all. As of now, the workaround @exah describes above is working well for me when I render these components separately during the Layout rendering when the window is not yet available. Some Docusuarus components are SSR safe, but others which rely on Scroll Context or Announcement Context etc do not seem to be at the moment. Just sharing for anyone else facing this challenge.
(swizzled theme-common Layout):
RudraSen2 commentedon Jan 23, 2023
I'm commenting on this without thinking - we can use https://stitches.dev
slorber commentedon May 4, 2023
The usage of React Server Components is growing, and we plan to adopt them in Docusaurus.
And we see that the past generation of runtime-based CSS-in-JS libs does not play with React Server Components (more explanations here: #8959).
At this point I'm not even sure we need to have these hooks anymore, it goes against the industry trend to adopt no-runtime solutions.
Many existing libs are in the process of figuring out how to benefit most from Server Components, so I think we should wait and decide later what is best for Docusaurus.
For the moment, I'm leaning towards not implementing these hooks because I believe it wouldn't encourage the usage of performant code by default in the long term, and the only good reason to implement those is to be compatible with legacy UI libs.
hacky way mentioned here meta/docusaurus#3236 (comment)