Why is getInitialProps deprecated? #13864
-
Hey guys, I'm very new to NextJS, but I'm building my first site/app with it right now, learning the ropes. Love everything so far. One thing I noticed is that in the docs for getInitialProps it recommends me to use getStaticProps or getServerSideProps instead. For what I understand getStaticProps is static rendering, running only at build time, a la GatsbyJS, and getServerSideProps does essentially the same thing, but executing at runtime instead, so on every request. Both run exclusively on the server though. getInitialProps runs either server-side, or client-side, depending on where the navigation took place. It seems that neither getStaticProps, or getServerSideProps is a real replacement for getInitialProps in that regard. I have now implemented my routes with getServerSideProps. but I notice a delay in clicking a link and the page rendering when I navigate client-side with this setup. Rather than seeing the skeleton of the page with a spinner while any requests are executed it just seems to wait until the server is finished creating my page before showing any feedback of the click. It would seems that in this situation getInitialProps would still be more suitable, because it will run server side if it's the first page being rendered, or client side in case of clicking a link within your app. This way I can show a spinner if it's not done loading yet. Or am I missing something entirely, where getServerSideProps can serve a skeleton page just like you would with client side navigation? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hi, welcome! With the new data-fetching methods, Next.js can easily detected server-side code and remove it from the client-side bundle, check this tool: https://next-code-elimination.now.sh
Next.js won't render the new page until the response from |
Beta Was this translation helpful? Give feedback.
-
It's not deprecated, but it's harder to manage, especially for beginners, there are more edge cases to be careful at. |
Beta Was this translation helpful? Give feedback.
Hi, welcome!
getInitialProps
runs both on the client and server side, this caused some confusion and required extra work to make sure unnecessary code wouldn't be bundled to the client.With the new data-fetching methods, Next.js can easily detected server-side code and remove it from the client-side bundle, check this tool: https://next-code-elimination.now.sh
The new separated methods are also important to signalize if the page should be static or server generated.
Next.js won't render the new page until the response from
g…