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

Upon Next v6 upgrade, global styles got broken #454

Closed
voxmachina opened this issue Jun 11, 2018 · 6 comments
Closed

Upon Next v6 upgrade, global styles got broken #454

voxmachina opened this issue Jun 11, 2018 · 6 comments

Comments

@voxmachina
Copy link

voxmachina commented Jun 11, 2018

Do you want to request a feature or report a bug?

I want to report a Bug

What is the current behavior?

When upgrading to Next v6.0.3, my global styles have stopped working displaying the message: "styles is not defined", I'm currently using styled-jsx with styled-jsx-css-loader

If the current behavior is a bug, please provide the steps to reproduce and possibly a minimal demo or testcase in the form of a Next.js app, CodeSandbox URL or similar

  1. Upgrade application to Next 6.0.3
  2. Follow the steps mentioned at the following link to update the Redux dependencies also: https://github.com/kirill-konshin/next-redux-wrapper#upgrade

Git repo with the use case in action, simple setup from scratch and it fails exactly the same:
https://github.com/voxmachina/nextjs-6-with-styled-jsx-demo

What is the expected behavior?

I would expect that the stylesheets would behave the same way as before and not break, note that only the "global" ones are failing, the others (scopped) are working ok as before.

Environment (include versions)

  • OS: macOS High Sierra (10.13.4)
  • Browser: Chrome 66.0.3359.181
  • styled-jsx (version): 2.2.6

Did this work in previous versions?

Yes, it did work before the upgrade

@blackbing
Copy link

blackbing commented Jun 11, 2018

I have similar problem. vercel/next.js#4239 (comment) has explained the correct way to config babel. But it can not be solved when running with jest.

check out this. vercel/next.js#4501 shows yarn dev and yarn build works correctly. but yarn test will break.

But I have no idea to address the problem.

@voxmachina
Copy link
Author

Yap, thanks for the reference, just tried now @blackbing still the same error, doesn't solve it for me either :/

@giuseppeg
Copy link
Collaborator

it might be an issue with Babel 7, I think that a temporary workaround is to use CommonJS exports instead of ES6:

module.exports = css`
  div { color: red }
`

@N4G1B4T0R
Copy link

Hi Everyone! I tried to figure out this issue. How said @giuseppeg there is problem with Babel 7 and after 2 days I got it. So I just create "crutch". Here is my _document.js

import React from 'react';
import Document, { Head, Main, NextScript } from 'next/document';
import JssProvider from 'react-jss/lib/JssProvider';
import flush from 'styled-jsx/server';
import getPageContext from '../src/getPageContext';
import baseStyles from '../styles/style.scss'

class MyDocument extends Document {
    render() {
        const { pageContext } = this.props;

        return (
            <html lang="en" dir="ltr" id="html">
            <Head>
                <title>InstaLazier</title>
                <meta charSet="utf-8" />
                {/* Use minimum-scale=1 to enable GPU rasterization */}
                <meta
                    name="viewport"
                    content={
                        'user-scalable=0, initial-scale=1, ' +
                        'minimum-scale=1, width=device-width, height=device-height'
                    }
                />
                {/* PWA primary color */}
                <meta name="theme-color" content={pageContext.theme.palette.primary.main} />
                <link
                    rel="stylesheet"
                    href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"
                />

            </Head>
            <body>
            <Main />
            <NextScript />
            </body>
            </html>
        );
    }
}

MyDocument.getInitialProps = ctx => {
    // Resolution order
    //
    // On the server:
    // 1. page.getInitialProps
    // 2. document.getInitialProps
    // 3. page.render
    // 4. document.render
    //
    // On the server with error:
    // 2. document.getInitialProps
    // 3. page.render
    // 4. document.render
    //
    // On the client
    // 1. page.getInitialProps
    // 3. page.render

    // Get the context of the page to collected side effects.
    const pageContext = getPageContext();
    const page = ctx.renderPage(Component => props => (
        <JssProvider
            registry={pageContext.sheetsRegistry}
            generateClassName={pageContext.generateClassName}
        >
            <Component pageContext={pageContext} {...props} />
        </JssProvider>
    ));
   // const test = "<style>html{font-family:'Roboto',sans-serif;}body,h1,h2,h3,h4,h5,h6{font-size:15px;margin:0;line-height:24px;}body{margin:0;background-color:#FAFAFD;overflow-x:hidden;}body,html,#__next,#__next>div{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;width:100%;overflow-y:auto;overflow-x:hidden;}#next{background-color:red;}#__next,#__next>div,body,html{height:100%;}a{color:transparent;-webkit-text-decoration:none;text-decoration:none;}a:hover{-webkit-text-decoration:underline;text-decoration:underline;}.b-icon{max-width:16px;width:100%;height:auto;}.b-icon--margin{margin-right:20px;}</style>"

    return {
        ...page,
        pageContext,
        styles: (
            <React.Fragment>
                <style
                    id="jss-server-side"
                    // eslint-disable-next-line react/no-danger
                    dangerouslySetInnerHTML={{ __html: pageContext.sheetsRegistry.toString() }}
                />
                <style
                    dangerouslySetInnerHTML={{ __html: baseStyles.toString() }}
                />
                {flush() || null}

            </React.Fragment>
        ),
    };
};

export default MyDocument;

So I just added manually second

<style dangerouslySetInnerHTML={{ __html: baseStyles.toString() }/>

And after that everything work nice on next 6.0.3.
I dont know how it will work in production but i guess babel will figure out this issue in the near future.

@giuseppeg
Copy link
Collaborator

Probably we are re using AST nodes which instead we should clone.

@giuseppeg
Copy link
Collaborator

this was fixed in #470

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

No branches or pull requests

4 participants