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

Layout broken after page reload (using with-styletron) #21825

Closed
Deliaz opened this issue Feb 3, 2021 · 4 comments · Fixed by #21908
Closed

Layout broken after page reload (using with-styletron) #21825

Deliaz opened this issue Feb 3, 2021 · 4 comments · Fixed by #21908
Labels
bug Issue was opened via the bug report template. examples Issue was opened via the examples template.

Comments

@Deliaz
Copy link

Deliaz commented Feb 3, 2021

What example does this report relate to?

with-styletron

What version of Next.js are you using?

"latest"

What version of Node.js are you using?

v12.13.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

yarn dev

Describe the Bug

I've scaffolded a simple website with two layouts based on "with-styletron".
Everything works fine on browser rendering, but the layout breaks after reloading [id].js page.
Feels like there is some inconsistency between SSR and probably hydration.

The same issue happened with a Header component in a different project, which also based on "with-styletron" example and has multiple layouts.

Expected Behavior

(see reproduce section for details)
Expected that the layout stays persistent.

To Reproduce

  1. Clone https://github.com/Deliaz/nextjs-styletron-issue
  2. go to the index page, input any number, and press enter
  3. observe sidebar on the left side
  4. reload the page
  5. observe sidebar to be center-shfted (page used the wrong layout)

ezgif com-gif-maker

@Deliaz Deliaz added bug Issue was opened via the bug report template. examples Issue was opened via the examples template. labels Feb 3, 2021
@giraffesyo
Copy link
Contributor

giraffesyo commented Feb 3, 2021

If you change your _document.js like so, it will resolve your problem.

import Document, { Html, Head, Main, NextScript } from 'next/document';
import { Provider as StyletronProvider } from 'styletron-react';

import styletron from '../styletron';

const RESET_STYLES = `
html,body {min-height:100vh;}
body{margin:0;}
`;

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const originalRenderPage = ctx.renderPage;

    ctx.renderPage = () => originalRenderPage({
      enhanceApp: (App) => (props) => <StyletronProvider value={styletron}><App {...props} /></StyletronProvider>,
    });

    const initialProps = await Document.getInitialProps(ctx);

    const stylesheets = styletron.getStylesheets() || [];

    return { ...initialProps, stylesheets };
  }

  render() {
    return (
      <Html>
        <Head>
          {this.props.stylesheets.map((sheet, i) => (
            <style
              className="_styletron_hydrate_"
              dangerouslySetInnerHTML={{ __html: sheet.css }}
              media={sheet.attrs.media}
              data-hydrate={sheet.attrs['data-hydrate']}
              key={i}
            />
          ))}
          <style type="text/css">{RESET_STYLES}</style>
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

@Deliaz
Copy link
Author

Deliaz commented Feb 3, 2021

It solved the issue, Thank you!

May I wonder, how did you come up with this solution?
And is there a guess why such detail isn't included in the official example?

@giraffesyo
Copy link
Contributor

giraffesyo commented Feb 6, 2021

It solved the issue, Thank you!

May I wonder, how did you come up with this solution?
And is there a guess why such detail isn't included in the official example?

Great, I'm glad it fixed the issue!

I cloned your reproduction repo and saw that Layout wasn't being populated on page load, that is why there was a difference between the client side routing and the server side rendering. I submitted a PR to see if we can get this included in the example.

giraffesyo added a commit to giraffesyo/next.js that referenced this issue Feb 6, 2021
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. examples Issue was opened via the examples template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants