getInitialProps is a legacy API
Unanswered
Hilsa shad posted this in #help-forum
Hilsa shadOP
Hello everyone, I am working on upgrading the Next.js version from Next 12 to Next 14. I want to stay with the page router architecture, but I am facing a challenge because in Next 14, getInitialProps is a legacy API. Instead of getInitialProps, I can use getStaticProps or getServerSideProps. However, I have a challenge because getStaticProps or getServerSideProps functions are triggered exclusively on the server, unlike getInitialProps. I have a project requirement where some functions need to be triggered on both SSR and the client. Has anyone had similar challenges? Is there any alternative solution? I do not want the solution to be calling the same functions on the client for every component where it is needed. Are there any best practices? It doesn’t make sense to me that with the deprecation of getInitialProps and its advantages, Next.js doesn’t have an alternative solution. The project is huge with many pages, components, lines of code, and logic 🙂 I am looking for the best possible solution for my project
7 Replies
@Hilsa shad Hello everyone, I am working on upgrading the Next.js version from Next 12 to Next 14. I want to stay with the page router architecture, but I am facing a challenge because in Next 14, getInitialProps is a legacy API. Instead of getInitialProps, I can use getStaticProps or getServerSideProps. However, I have a challenge because getStaticProps or getServerSideProps functions are triggered exclusively on the server, unlike getInitialProps. I have a project requirement where some functions need to be triggered on both SSR and the client. Has anyone had similar challenges? Is there any alternative solution? I do not want the solution to be calling the same functions on the client for every component where it is needed. Are there any best practices? It doesn’t make sense to me that with the deprecation of getInitialProps and its advantages, Next.js doesn’t have an alternative solution. The project is huge with many pages, components, lines of code, and logic 🙂 I am looking for the best possible solution for my project
@Hilsa shad Sorry but I couldn't fully understand your "pain".
What kind of things you need to run on the server and the client side again?
What kind of things you need to run on the server and the client side again?
Hilsa shadOP
I apologize for the incomplete question. When I mentioned triggering some function, I didn’t mean a request to the backend. Actually, my main concern is that on the SSR side, I want to populate data in the Redux state, so that I can use the same data on the client side. And in the reverse case, when I navigate to another page, I want to be able to use data from the Redux store on the SSR side, for example, if I need data for some condition. Can getServerSideProps cause problems?
hmm, @Hilsa shad redux is never on the server side!
for the redux states, you can do client-side data fetching and store it in the redux
and in other pages, in the client side, you can reuse it
Hilsa shadOP
@James4u Do you think I can’t dispatch a Redux action on the SSR side?
Hilsa shadOP
When I return to a page I have already visited, it is necessary to trigger an action without refreshing the page. getInitialProps allows this, unlike the getServerSideProps function.