styled-jsx does not render server side with custom _document.js
I am using styled-jsx throughout my app and I am getting a flicker when the server loads the initial page. This only happens when I use a custom _document.js, so it must be something with that page.
This is the code:
import Document, { Head, Main, NextScript } from 'next/document'
import GoogleTagManager from '../utils/GTM'
import flush from 'styled-jsx/server'
export default class MyDocument extends Document {
static async getInitialProps ({ renderPage }) {
const { html, head } = renderPage()
const styles = flush()
return { html, head, styles }
}
render () {
console.log('styles: ', this.props.styles)
return (
<html>
<head>
<script src={`https://maps.googleapis.com/maps/api/js?key=${GOOGLE_KEY}`} />
<meta charSet="utf-8" />
{this.props.styles}
</head>
<body className="body">
<GoogleTagManager gtmId='GTM-xxx' />
<div id="fb-root" />
<Main />
<NextScript />
</body>
</html>
)
}
}
Output:
styles: []
styles: []
The console output for props.style is always empty arrays. Should this be done in a different way? I'm unsure what to do as the docs seem to say this is all that is needed.
Your Environment
| Tech | Version |
|---|---|
| next | 2.4.6 |
| node | 7.7.4 |
| OS | w10/macos/centos |
| browser | all |
| etc |
I moved everything from _document.js into my Layout components and everything is working as expected.
I did some logging inside style.js from styled-jsx and it doesn't look like it was finding any components to add to stylesMap.
Try to use this as getInitialProps:
static async getInitialProps (ctx) {
return Document.getInitialProps (ctx)
}
static getInitialProps ({ renderPage }) {
const {html, head, errorHtml, chunks} = renderPage()
const styles = flush()
return { html, head, errorHtml, chunks, styles }
}
The document.js signature has changed. Looks like the above now. That's why Arunoda is suggesting calling the parent 👍
Okay so using
static async getInitialProps (ctx) {
return Document.getInitialProps (ctx)
}
does work, I have access to a styles prop.
Hi @mattfca, @timneutkens, @arunoda
I got the same issue
This is my getInitialProps:
static async getInitialProps(ctx) {
const { html, head, errorHtml, chunks } = renderPage();
return { html, head, errorHtml, chunks };
}
And changing it to static async getInitialProps (ctx) { return Document.getInitialProps (ctx) }
solves it. But can you please explain why? Im using latest NextJS.
Also where can I find documentation about document.js and all its capabilities and features?
Thanks!
This is really weird... When I rewrite getInitialProps with exact same code as original Document, I can not get styles:
static getInitialProps ({ renderPage }) {
const { html, head, errorHtml, chunks } = renderPage();
const styles = flush();
return { html, head, errorHtml, chunks, styles };
}
When I use original Document.getInitialProps or not to rewrite it, I can get styles. Can anyone let me know why? Thanks!
Out of curiosity, is it necessary to call const styles = flush(); for styled jsx? @arunoda @timneutkens
@pruhstal it's the way to flush all the styles into a string and make sure they're rendered server side, so yes, if you're using styled-jsx. If you're not using styled-jsx at all you don't need it.