bjoluc / next-redux-cookie-wrapper

Sync a subset of your Redux state with cookies in Next.js :cookie: :sparkles:
MIT License
114 stars 4 forks source link

Error: Text content does not match server-rendered HTML in newer NextJS version #36

Closed heinthanth closed 2 years ago

heinthanth commented 2 years ago

I've cloned the repo ( demo repo ). Upgraded all dependencies. And the demo no longer works resulting in Content mis-match as below.

Screen Shot 2022-06-17 at 3 37 24 PM

The clean ( fresh ) NextJS application also not working.

heinthanth commented 2 years ago

Here's updated versions.

Screen Shot 2022-06-17 at 3 39 47 PM
heinthanth commented 2 years ago

Clean demo ( with Next 12 and Wrapper 7 ) also not working.

uwemneku commented 2 years ago

@heinthanth calling wrapper.getInitialPageProps((store) => () => {}) in pages without getSeverSideProps fixed this issue for me

heinthanth commented 2 years ago

Oh ... interesting ... I think we can do that in _app.tsx too. Calling that in every pages seems duplicated.

My use-case is that, I store site settings ( locale, theme ) in cookie and if user request dark theme, dark theme is responded from server side, So that, user won't see flashes, etc.

heinthanth commented 2 years ago
image

Yeah, I added something like this and it works.

uwemneku commented 2 years ago

@heinthanth This definately looks neater. My only concern is that next js docs gives this waring

This disables the ability to // perform automatic static optimization, causing every page in your app to // be server-side rendered

Although from the How it works documentation for next-redux-wrapper, it does mention that getInitialProps is called in _app.js so maybe this concern is not valid

bjoluc commented 2 years ago

Hi both,

the runtime error is thrown by React v18. When Next.js pre-renders a page, no client cookies are available (the page is rendered for all clients after all). Hence, the default state is included in the server-rendered HTML, whereas the initial client render has access to the client's cookies and thus differs from the server render. There's nothing we can really do about this, except for server-rendering affected pages per-client (using getServerSideProps or getInitialProps). The mismatch error only occurs on the page that is initially loaded though, so I think I'll add a "/" => "/ssr/1" redirect to the demo to circumvent it in the demo project.

Your concern is right @uwemneku, getInitialProps in _app.js disables automatic static optimization so the page is server-rendered for each request individually. I'd consider it only as a last resort...

heinthanth commented 2 years ago

Yeah ... I think I should close this issue now. @bjoluc's answer does make sense.

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 2.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: