styled-jsx does not render server side with custom _document.js
See original GitHub issueI 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 |
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
styled-jsx does not render server side with custom _document.js
This only happens when I use a custom _document.js, so it must be something with that page. This is the code: import Document,...
Read more >Advanced Features: Custom `Document` - Next.js
Note: This is advanced and only needed for libraries like CSS-in-JS to support server-side rendering. This is not needed for built-in styled-jsx support....
Read more >next.js - The styled-jsx does not render the ... - Stack Overflow
The styled-jsx does not render the style when the Browser has js disabled (Rollup and styled-jsx) · Enter in the root project and...
Read more >Server-side rendered styled-components with Nextjs
Basically you need to add a custom pages/_document.js (if you don't have one). Then copy the logic for styled-components to inject the server...
Read more >Server side rendering Styled-Components with NextJS - Medium
If you disable JavaScript on the browser (e.g in Chrome: Settings / Site settings / JavaScript / Blocked), you should still be able...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
Try to use this as getInitialProps: