next.js icon indicating copy to clipboard operation
next.js copied to clipboard

styled-jsx does not render server side with custom _document.js

Open mattfca opened this issue 9 years ago • 8 comments

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

mattfca avatar Jul 19 '17 00:07 mattfca

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.

mattfca avatar Jul 19 '17 04:07 mattfca

Try to use this as getInitialProps:

    static async getInitialProps (ctx) {
      return Document.getInitialProps (ctx)
    }

arunoda avatar Jul 19 '17 05:07 arunoda

  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 👍

timneutkens avatar Jul 19 '17 08:07 timneutkens

Okay so using

static async getInitialProps (ctx) {
      return Document.getInitialProps (ctx)
    }

does work, I have access to a styles prop.

mattfca avatar Jul 20 '17 00:07 mattfca

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!

aviramga avatar Oct 16 '17 13:10 aviramga

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!

zation avatar Oct 25 '17 10:10 zation

Out of curiosity, is it necessary to call const styles = flush(); for styled jsx? @arunoda @timneutkens

pruhstal avatar Oct 25 '17 17:10 pruhstal

@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.

timneutkens avatar Oct 25 '17 17:10 timneutkens