You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. I've been using Next.js a lot recently and have been enjoying just how much easier it makes dealing with SEO and other server side related pains.
I am however confused on what the best way to handle state management is supposed to be though. I've found myself always relying on getInitialProps which feels weird as according to your documentation it's recommended to use getServerSideProps instead.
The problem with getServerSideProps however is that it issues a request to the server each time, whilst with getInitialProps it runs on the client and can thus re-use data that was fetched before.
Let's say you have a post, with a user. The page for a post fetches the post by its id, and this is stored in a Redux or MobX (whatever you use) store with getInitialProps. Then, a link on the page directs you to the page for that user, which given proper state management, will load instantly because the user was fetched along with the post.
But you can't do this with getServerSideProps, as it runs on the server and said data will not be "cached" on the server (it shouldn't be, either). This means that navigating to data that is in theory already fetched and available still has a delay (the request to the server), which is undesirable in a lot of cases where UX can be improved by avoiding such delays (especially on a bad connection).
I just don't understand how to deal with this, because since getInitialProps is "discouraged" and also doesn't allow for elegant handling of redirects and 404 (you have to implement it yourself with helper functions), it feels wrong to use it even though in my case it's the most logical thing to use. I have a site already running in production that uses only getInitialProps and it works flawlessly, but it feels like I'm not using Next.js as intended.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Hi. I've been using Next.js a lot recently and have been enjoying just how much easier it makes dealing with SEO and other server side related pains.
I am however confused on what the best way to handle state management is supposed to be though. I've found myself always relying on
getInitialPropswhich feels weird as according to your documentation it's recommended to usegetServerSidePropsinstead.The problem with
getServerSidePropshowever is that it issues a request to the server each time, whilst withgetInitialPropsit runs on the client and can thus re-use data that was fetched before.Let's say you have a post, with a user. The page for a post fetches the post by its id, and this is stored in a Redux or MobX (whatever you use) store with
getInitialProps. Then, a link on the page directs you to the page for that user, which given proper state management, will load instantly because the user was fetched along with the post.But you can't do this with
getServerSideProps, as it runs on the server and said data will not be "cached" on the server (it shouldn't be, either). This means that navigating to data that is in theory already fetched and available still has a delay (the request to the server), which is undesirable in a lot of cases where UX can be improved by avoiding such delays (especially on a bad connection).I just don't understand how to deal with this, because since
getInitialPropsis "discouraged" and also doesn't allow for elegant handling of redirects and 404 (you have to implement it yourself with helper functions), it feels wrong to use it even though in my case it's the most logical thing to use. I have a site already running in production that uses onlygetInitialPropsand it works flawlessly, but it feels like I'm not using Next.js as intended.All reactions