Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mattfca opened this issue Jul 19, 2017 · 8 comments
Closed

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

mattfca opened this issue Jul 19, 2017 · 8 comments

Comments

@mattfca
Copy link

mattfca commented Jul 19, 2017

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
Copy link
Author

mattfca commented Jul 19, 2017

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.

@arunoda
Copy link
Contributor

arunoda commented Jul 19, 2017

Try to use this as getInitialProps:

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

@timneutkens
Copy link
Member

  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 👍

@mattfca
Copy link
Author

mattfca commented Jul 20, 2017

Okay so using

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

does work, I have access to a styles prop.

@mattfca mattfca closed this as completed Jul 20, 2017
@aviramga
Copy link

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!

@zation
Copy link

zation commented Oct 25, 2017

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!

@pruhstal
Copy link

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

@timneutkens
Copy link
Member

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants