Created
May 21, 2020 08:53
-
-
Save dburles/66e53feec0510cfbdfce539ff773d74e to your computer and use it in GitHub Desktop.
Next.js configuration for Mystical v2
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 { MysticalProvider } from 'mystical'; | |
import PropTypes from 'prop-types'; | |
import theme from '../lib/theme'; | |
const App = ({ Component, pageProps, cache }) => { | |
return ( | |
<MysticalProvider theme={theme} cache={cache}> | |
<Component {...pageProps} /> | |
</MysticalProvider> | |
); | |
}; | |
App.propTypes = { | |
Component: PropTypes.elementType.isRequired, | |
pageProps: PropTypes.object, | |
cache: PropTypes.object, | |
}; | |
export default 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
import { createCache } from 'mystical'; | |
import Document, { Head, Html, Main, NextScript } from 'next/document'; | |
class MyDocument extends Document { | |
static async getInitialProps(ctx) { | |
const cache = createCache(); | |
const originalRenderPage = ctx.renderPage; | |
ctx.renderPage = () => { | |
return originalRenderPage({ | |
enhanceApp(App) { | |
// eslint-disable-next-line react/display-name | |
return (props) => { | |
return <App {...props} cache={cache} />; | |
}; | |
}, | |
}); | |
}; | |
const initialProps = await Document.getInitialProps(ctx); | |
const { css, identifiers } = cache.getServerStyles(); | |
return { css, identifiers, ...initialProps }; | |
} | |
render() { | |
return ( | |
<Html> | |
<Head> | |
<script id="__mystical__" data-identifiers={this.props.identifiers} /> | |
{this.props.css.map(({ id, rules }) => { | |
return ( | |
<style key={id} dangerouslySetInnerHTML={{ __html: rules }} /> | |
); | |
})} | |
</Head> | |
<body> | |
<Main /> | |
<NextScript /> | |
</body> | |
</Html> | |
); | |
} | |
} | |
export default MyDocument; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment