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

SSR store missing cookie data in Next 13 #44

Open
wbobeirne opened this issue Nov 9, 2022 · 7 comments
Open

SSR store missing cookie data in Next 13 #44

wbobeirne opened this issue Nov 9, 2022 · 7 comments

Comments

@wbobeirne
Copy link

Hey there, I've copied the example code identically but using next@^13, and I'm getting an hydration mismatch. It looks like my store is being initialized empty despite the cookies existing:

4. useWrappedStore created new store with { initialState: undefined, initialStateFromGSPorGSSR: undefined

Were there any breaking changes in 13?

@bjoluc
Copy link
Owner

bjoluc commented Nov 15, 2022

Hi @wbobeirne,

I just updated the demo to next-redux-wrapper 8 and next 13 but was unable to reproduce the issue – unless temporarily when I forgot to remove wrapper.withRedux after adding wrapper.useWrappedStore in _app.ts 😄 Assuming that is not your problem, would you mind creating a minimal repro based on the demo project so I can take a look?

@abrouaux
Copy link

Hey @bjoluc, actually I'm also seeing a react hydration error on my end in the demo of this project.

To repro, just increase the counter on one of the getStaticProps page, then reload the page.
In this case, I don't see the hydration step as the first step done on the SSR store, therefore the HTML is generated with the default values, which React is picking on client-side as it's rendering with the cookie value.

This doesn't happen with the getServerSideProps pages.

@bjoluc
Copy link
Owner

bjoluc commented Jan 26, 2023

Hi @abrouaux,

In this case, I don't see the hydration step as the first step done on the SSR store, therefore the HTML is generated with the default values, which React is picking on client-side as it's rendering with the cookie value.

The behavior you describe is intended, I'm afraid: getStaticProps is run at build (/regenerate) time, not per request. Hence, it doesn't have a specific client's cookies available and can't hydrate the server-side store with cookie values. If you need this, make sure you are using getServerSideProps.

@abrouaux
Copy link

abrouaux commented Feb 2, 2023

@bjoluc aw crap, you're totally right - this fact completely slipped my mind.

An advice: it would help others to mention this in the various use cases of your example.
In hindsight, it's obvious that the getStaticProps pages are pre-rendered & then served "as they were".
But then the only interesting use-case for redux/cookie interactions is about SSR - via getServerSideProps.

Cheers!

@bjoluc
Copy link
Owner

bjoluc commented Feb 2, 2023

An advice: it would help others to mention this in the various use cases of your example.

That's a good point! Maybe we should add a little notice on the static example pages, explaining what happens. WDYT?

@abrouaux
Copy link

abrouaux commented Feb 2, 2023

Actually I'm not even sure you should provide "getStaticProps" cases in your example - the only value of storing redux data in a cookie is to pre-render stuff based on client data.

@bjoluc
Copy link
Owner

bjoluc commented Feb 21, 2023

the only value of storing redux data in a cookie is to pre-render stuff based on client data.

@abrouaux That's right. Nevertheless, it's important that state from getStaticProps doesn't affect client/cookie state for the configured subtrees, while still being applied to other subtrees. Static pages showcase just that...

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

3 participants