rollup-plugin-styles
rollup-plugin-styles copied to clipboard
Custom injector for server-side rendering (loosing treeshakeable ability)
Currently we need to use a custom injector to be able to retrieve all styles for SSR.
import originalInjector from 'rollup-plugin-styles/dist/runtime/inject-css';
const injectCss = (css) => {
if (typeof document === 'undefined') {
if (global.ssrStyles) global.ssrStyles.push(css);
else global.ssrStyles = [];
}
originalInjector(css, {
singleTag: true,
});
};
export default injectCss;
Then we get the global.ssrStyles on our Next.js _document.tsx
import Document, { Head, Html, Main, NextScript } from 'next/document';
export default class MyDocument extends Document {
static async getInitialProps(ctx: any) {
const initialProps = await Document.getInitialProps(ctx);
//@ts-ignore
const globalStyle = global.ssrStyles.join(' ');
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{[
<style
dangerouslySetInnerHTML={{ __html: globalStyle }}
id="server-side-styles"
/>,
]}
</>
),
};
}
render() {
return (
<Html lang="fr">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
My rollup-plugin-styles config (preserveModules enabled):
styles({
autoModules: true,
minimize: true,
mode: [
'inject',
(varname) => {
const pathInjector = path.resolve('./tools/rollup/inject-css.js');
return ` import injectCss from '${pathInjector}';injectCss(${varname})`;
},
],
}),
I didn't find a better way to properly inject my css on server-side. But with this approach we completely loose the ability to use the treeshakeable option.
Do you think that there is a better way to inject our css on server-side render ?
Any help on this ? The only solution I see right now for me is to fork the réponse:(
Any update of this ?
Any update of this? I can help if is need it.
Bumping.
I'm also wondering how to solve this. Any updates?
I havent done server side rendering
However I've been playing around with using this plugin and @emotion/css for injection instead of the standard injection So I'm going to throw this here because it may spark ideas of using @emotion to do some server side rendering stuff
styles({
mode: [
"inject",
(varName) => `
const { injectGlobal } = require('@emotion/css');
injectGlobal(${varName});
`,
],
autoModules: true,
modules: true,
}),
You may be able to use docs from emotion to achieve what you're looking for https://emotion.sh/docs/ssr