Skip to content

Instantly share code, notes, and snippets.

@rschristian
Created February 18, 2022 20:00
Show Gist options
  • Save rschristian/1d814e3f4890854c375ed53269b8d9f7 to your computer and use it in GitHub Desktop.
Save rschristian/1d814e3f4890854c375ed53269b8d9f7 to your computer and use it in GitHub Desktop.
Styled Components SSR
import './style';
import App from './components/app';
let wrapper;
if (process.env.SSR) {
wrapper = (App) => {
const { ServerStyleSheet, StyleSheetManager } = require('styled-components');
const sheet = new ServerStyleSheet();
const StyleTags = () => sheet.getStyleElement();
return () => [
<StyleSheetManager sheet={sheet.instance}>
<App />
</StyleSheetManager>,
<StyleTags />
];
}
} else {
wrapper = (app) => app;
}
export default wrapper(App);
/**
* @param {import('preact-cli').Config} config
* @param {import('preact-cli').Env} env
* @param {import('preact-cli').Helpers} helpers
*/
export default (config, env, helpers) => {
const { rule } = helpers.getLoadersByName(config, 'babel')[0];
const babelConfig = rule.options;
babelConfig.plugins.push('babel-plugin-styled-components')
const { plugin } = helpers.getPluginsByName(config, 'DefinePlugin')[0];
plugin.definitions['process.env.SSR'] = env.ssr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment