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

[v4] gatsby-plugin-material-ui rearranges style order during hydration if <ThemeProvider> is used. #86

Open
nagiek opened this issue Nov 20, 2021 · 0 comments

Comments

@nagiek
Copy link

nagiek commented Nov 20, 2021

Problem
In the root gatsby-ssr.js and gatsby-browser.js, I had wrapped my element with @mui's <ThemeProvider>. This worked OK for gatsby-ssr.js - styles came out where expected.

However, on the initial client re-render, the emotion <style> tags would be rearranged and moved to be the first child (from the last). I assume it is due to getting a different theme. I could not get this to work by putting the <ThemeProvider> into its own plugin.

You can see the official implementation here:
https://mui.com/guides/server-rendering/#handling-the-request

This took a while to debug, hope it helps someone.

Old - within root gatsby-ssr.js:

export const wrapRootElement = (element) => {
  <ThemeProvider theme={theme}>
   {element}
  </ThemeProvider>
}

New - within new plugin (fork of this plugin):

export const wrapRootElement = (element) => {
  <CacheProvider cache={cache}>
    <ThemeProvider theme={theme}>
     {element}
    </ThemeProvider>
  </CacheProvider>
}

Using
@emotion/styles 11.6
@emotion/react 11.6
gatsby 3.14.1
gatsby-plugin-material-ui v.4.1.0

@nagiek nagiek changed the title gatsby-plugin-material-ui rearranges style order during hydration if <ThemeProvider> is used. [v4] gatsby-plugin-material-ui rearranges style order during hydration if <ThemeProvider> is used. Nov 20, 2021
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

1 participant