rsnext/examples/with-emotion/pages/_document.js
Matt 6ff89b1f52 Update with-emotion to flush SSR styles on refresh (#2565)
* Update with-emotion to flush SSR styles on refresh

* with-emtotion: remove @apply, use composes

* fix env determination
2017-07-15 09:32:14 +02:00

37 lines
865 B
JavaScript

import Document, { Head, Main, NextScript } from 'next/document'
import { extractCritical } from 'emotion/server'
import { flush } from 'emotion'
const dev = process.env.NODE_ENV !== 'production'
export default class MyDocument extends Document {
static getInitialProps ({ renderPage }) {
if (dev) { flush() }
const page = renderPage()
const styles = extractCritical(page.html)
return { ...page, ...styles }
}
constructor (props) {
super(props)
const { __NEXT_DATA__, ids } = props
if (ids) {
__NEXT_DATA__.ids = this.props.ids
}
}
render () {
return (
<html>
<Head>
<title>With Emotion</title>
<style dangerouslySetInnerHTML={{ __html: this.props.css }} />
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
)
}
}