Skip to content

Instantly share code, notes, and snippets.

@dburles
Created May 19, 2020 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dburles/122997990e41504e7733eb91db68f62e to your computer and use it in GitHub Desktop.
Save dburles/122997990e41504e7733eb91db68f62e to your computer and use it in GitHub Desktop.
Next.js configuration for Mystical
import { MysticalProvider } from 'mystical';
import PropTypes from 'prop-types';
const App = ({ Component, pageProps, cache }) => {
return (
<MysticalProvider cache={cache}>
<Component {...pageProps} />
</MysticalProvider>
);
};
App.propTypes = {
Component: PropTypes.elementType.isRequired,
pageProps: PropTypes.object,
cache: PropTypes.object,
};
export default App;
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>
<style
id="__mystical__"
data-identifiers={this.props.identifiers.join(',')}
dangerouslySetInnerHTML={{ __html: this.props.css }}
/>
</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