Created
February 18, 2022 20:00
-
-
Save rschristian/1d814e3f4890854c375ed53269b8d9f7 to your computer and use it in GitHub Desktop.
Styled Components SSR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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