Looking to load only critical css in the head #28044
Replies: 4 comments 1 reply
-
Looking to do this as well. |
Beta Was this translation helpful? Give feedback.
-
There are few approaches here:
|
Beta Was this translation helpful? Give feedback.
-
On the same note, why is CSS in-lining done via Gatsby means? Over webpack It took me a while to debug why all css files used by the page are being inlined into the html regardless of my webpack config. Curious about the reasoning behind inlining-by-default approach as well. I think number of simultaneous requests is not much of an issue with recent browsers and HTTP2. Kinda related to #28046. |
Beta Was this translation helpful? Give feedback.
-
In case someone else out there needs a copy-paste quick solution for an older version...
export const onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
replaceHeadComponents(
getHeadComponents().sort((a) => (a.type && a.type === `meta` ? -1 : 0))
);
}; |
Beta Was this translation helpful? Give feedback.
-
I'm looking to build a plugin to extract css from the head and insert only the critical css. In order to do this, it seems that I would need access to all three:
getHeadComponents
,replacedHeadComponents
, andbodyComponent
in the same server rendering API functions. Reason being, I need to 1. access the inline styles in the head, 2. use those styles and the body to generate critical css, and 3. replace the head component with only critical css and a link to the external stylesheet.First, it does not appear this is possible given the current server rendering APIs. Can anyone confirm?
Second, am I missing a more obvious path to do this?
I'm relatively new to the Gatsby world. Any help you can offer would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions