Any way to retrieve cookie on server side?
thihathit opened this issue ยท 3 comments
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.
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...
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!
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.