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

Hydrate state from cookies without server side rendering #22

Closed
MonsterDeveloper opened this issue Nov 29, 2021 · 6 comments
Closed

Hydrate state from cookies without server side rendering #22

MonsterDeveloper opened this issue Nov 29, 2021 · 6 comments
Labels
bug Something isn't working released

Comments

@MonsterDeveloper
Copy link

Hi! I'm trying to implement this library, and it works great, when the page has some SSR methods (like getServerSideProps or others), however, I am missing one important function - it can't hydrate the state from cookies when the page doesn't have SSR.

So I have some state in cookies, and it hydrates perfectly when page has gSSP method. However, when there are no SSR methods on page, next-redux-wrapper doesn't call HYDRATE action and the state is not fetched from cookies.

My question is: can I hydrate the state from cookies on client-side without using gSSP or other server-side rendering methods?

@MonsterDeveloper
Copy link
Author

As a quick workaround I've created an empty getStaticProps method, which enables state hydration from cookies and doesn't disable Next.js static optimization:

const emptyGetStaticProps = wrapper.getStaticProps(() => () => ({
  props: {},
}));

// in Page: export const getStaticProps = emptyGetStaticProps

@bjoluc bjoluc mentioned this issue Dec 2, 2021
@bjoluc
Copy link
Owner

bjoluc commented Dec 2, 2021

Hi @MonsterDeveloper,
thanks for opening this issue! Your use case is very valid and should definitely be supported. The relevant section regarding this in next-redux-wrapper is here. Now I'm wondering: Despite the initial client-side render, is there any moment where the state should be read from cookies even though a page doesn't have "SSR methods"? If not, maybe we can find a way to simply dispatch a HYDRATE action in next-redux-cookie-wrapper when the app loads and state cookies are present. This would obviously result in a duplicate HYDRATE action being dispatched on initial page loads of pages that have said methods. Maybe there's some workaround for that, but I'm not sure how it might look like. Wdyt?

@bjoluc bjoluc added the bug Something isn't working label Dec 2, 2021
@MonsterDeveloper
Copy link
Author

I’ve also thought about dispatching hydrate action, but I’m not sure where to take this initial data from.

So I think some modifications can be made:
on initial page load, check if page doesn’t have gSSP, gSP or gIP. If it does, continue as normal. Otherwise read state from cookies and dispatch hydrate action.

Also we don’t need to hydrate state from cookies when navigating pages after initial load.

Do you think it’s possible to create such modifications?

@bjoluc
Copy link
Owner

bjoluc commented Dec 13, 2021

Also we don’t need to hydrate state from cookies when navigating pages after initial load.

Great, that's what I wanted to hear 😄

on initial page load, check if page doesn’t have gSSP, gSP or gIP.

That's the problem: We're just a Redux middleware and there's no way for us to simply tell if the current page has these methods – at least none that I know of. I also checked the context argument that next-redux-wrapper provides to the makeStore function: It's always an empty object on the client side. Hence, I think the best chance we have is to always dispatch an initial HYDRATE action when a store is created on the client side. If there's gSSP, gSP or gIP on the first page a client visits, then there would be two HYDRATE actions: One with the up-to-date cookie data, and one with gSSP, gSP or gIP and cookie data. I don't think that's much of a problem though.

@bjoluc bjoluc closed this as completed in 8246f5c Dec 13, 2021
github-actions bot pushed a commit that referenced this issue Dec 13, 2021
### [2.1.2](v2.1.1...v2.1.2) (2021-12-13)

### Bug Fixes

* Fix client-side hydration on pages without server-side data fetching methods ([8246f5c](8246f5c)), closes [#22](#22)
@github-actions
Copy link

🎉 This issue has been resolved in version 2.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@MonsterDeveloper
Copy link
Author

Thank you! I’ll try it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

2 participants