提交 1b893518 编写于 作者: G Grokling 提交者: Tim Neutkens

Add SSR to examples/with-styled-components + bump styled_components v2 (#1908)

上级 12f423fa
{
"presets": [
"next/babel",
"stage-0"
],
"plugins": [
["styled-components", { "ssr": true, "displayName": true, "preprocess": false } ]
]
}
\ No newline at end of file
......@@ -7,10 +7,12 @@
"start": "next start"
},
"dependencies": {
"babel-plugin-styled-components": "^1.1.4",
"babel-preset-stage-0": "^6.24.1",
"next": "latest",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"styled-components": "^1.4.4"
"styled-components": "^2.0.0-17"
},
"author": "",
"license": "ISC"
......
import Document, { Head, Main, NextScript } from 'next/document'
import styleSheet from 'styled-components/lib/models/StyleSheet'
import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document {
static async getInitialProps ({ renderPage }) {
const page = renderPage()
const styles = (
<style dangerouslySetInnerHTML={{ __html: styleSheet.rules().map(rule => rule.cssText).join('\n') }} />
)
return { ...page, styles }
}
render () {
const sheet = new ServerStyleSheet()
const main = sheet.collectStyles(<Main />)
const styleTags = sheet.getStyleElement()
return (
<html>
<Head>
<title>My page</title>
{styleTags}
</Head>
<body>
<Main />
<div className='root'>
{main}
</div>
<NextScript />
</body>
</html>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册