For detailed reference information about critical CSS, please refer to https://web.dev/articles/extract-critical-css.
Generally, one of our React components might look like this.
Frameworks like Next.js, or the mini-css-extract-plugin that people might use, will extract CSS into a separate file.
It has a significant drawback in that it extracts the CSS for any module you import, which can sometimes be unnecessary.
For example, in the case above, for people who are on the control side of the A/B test, the CSS for any other module is unnecessary.
We have built an internal wrapper around isomorphic-style-loader. Now, you can use the useStyles
hook to collect the CSS needed during the rendering process.
In order to distinguish it from external CSS, we only enable critical CSS collection for files ending in .iso.scss
.
Critical CSS can be used with stream rendering, but it's important to note that our useStyles calls must be placed before the use function, otherwise these styles cannot be collected on the server side.