Skip to content

Instantly share code, notes, and snippets.

@mabhub
Created November 21, 2019 16:57
Show Gist options
  • Save mabhub/29e4f75cbee007087824b4acdee12702 to your computer and use it in GitHub Desktop.
Save mabhub/29e4f75cbee007087824b4acdee12702 to your computer and use it in GitHub Desktop.
Do not inline CSS in Gatsby rendered HTML files
exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
if (process.env.NODE_ENV !== 'production') {
return;
}
const headComponents = getHeadComponents();
replaceHeadComponents(headComponents.map(headComponent => {
if (headComponent.type !== 'style') {
return headComponent;
}
const {
props: {
dangerouslySetInnerHTML,
'data-href': href,
...props
},
} = headComponent;
return {
...headComponent,
type: 'link',
props: {
...props,
rel: 'stylesheet',
type: 'text/css',
href,
},
};
}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment