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

Any way to retrieve cookie on server side? #5

Closed
thihathit opened this issue Apr 29, 2020 · 3 comments
Closed

Any way to retrieve cookie on server side? #5

thihathit opened this issue Apr 29, 2020 · 3 comments
Labels
question Further information is requested

Comments

@thihathit
Copy link

First of all, this is a great wrapper. Many thanks for putting effort.
Any way to retrieve cookie on server side except req.headers.cookie ?
I'm using nextJS's new getServerSideProps function.

@bjoluc
Copy link
Owner

bjoluc commented Apr 30, 2020

Unfortunately not. I could maybe add a utility function to do so, but I don't think that makes much sense. Let me explain why:
next-redux-cookie-wrapper uses version 5 of next-redux-wrapper under the hood, which is driven by defining a getInitialProps() method for the App component. This means, every landing request will be server-rendered. In other words, there's no benefit of using getServerSideProps and getStaticProps instead of getInitialProps().
The future solution of next-redux-wrapper will use a different API and likely require each Page component, and each getServerSideProps or getStaticProps method to be wrapped. In addition to that, it will require writing a custom reducer (here's a discussion on that).
I like the set-and-forget mentality of next-redux-cookie-wrapper and I don't think there's a similarly easy-to-use approach like the current one that enables static optimization. Hence, I will most likely not implement support for the new Next.js functions in this library.

I do want to take another route though: Once vercel/next.js#9133 comes out, I would like to implement a universal plugin that fully manages Redux state throughout all pages, supporting getServerSideProps and getStaticProps, with a declarative configuration like this:

config = {
  "my.state.auth": "sync-persist" // synchronizes a state subtree between client and server (like next-redux-cookie-wrapper currently does)
  "another.state.subtree": "server" // client overrides its local state with fresh server state on page loads
  ...
}

There are plenty of ways to handle Redux state in a client-server scenario, and I would like to provide a set of reasonable default patterns, which can easily be applied to specific parts of the Redux state. This would insanely reduce the amount of manual boilerplate code...

@bjoluc
Copy link
Owner

bjoluc commented Apr 30, 2020

So as an answer to your question: Considering the current implementation, you're best off using getInitialProps to get (and possibly modify) the Redux state. Let me know if you have any questions regarding that!

@bjoluc bjoluc added the question Further information is requested label Apr 30, 2020
@thihathit
Copy link
Author

Thank you for the nice explanation, I'm new to next.js and this helps me a lot that I should use getInitialProps for now. I'll be closing for now since the problem solved for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants