Server-side data fetching in Next.js 12 vs. 13
Introduction
Data fetching is an essential aspect of web development, especially when it comes to creating dynamic and interactive applications. In this article, we will explore the differences between data fetching in Next.js 12 and Next.js 13, two popular versions of the Next.js framework. We will discuss how data can be fetched and shared between components, and how Next.js 13 introduces new features that simplify the process.
Next.js 12: Fetching Data in the Underscore App
In Next.js 12, data fetching in the underscore app requires some additional steps. Let’s consider an example where we have a Next.js 12 app with two routes: home and about. We want to add some dynamic data to our shared header component.
To fetch data in the underscore app, we need to go to the index page and use the `getServerSideProps` function. This function is asynchronous and allows us to fetch data before rendering the page. We can use the `getServerSideProps` function to await a call to retrieve user data, and then return the user as a prop.
Once we have the user data available, we can pass it to our header component by logging the `pageProps` in the app file. We can then access the user’s name property and pass it to the header component as a prop. Finally, in the header component, we can display a welcome message using the passed name prop.
However, if we visit the about page, we will encounter an error because the about page is not currently fetching data. To fix this, we need to copy the data fetching code from the index page to the about page. This introduces implicit coupling between each new page and our shared header component in the underscore app file.
Next.js 13: Fetching Data Inside Components
Next.js 13 introduces a more streamlined approach to data fetching. In this version, we can fetch data directly inside components, eliminating the need for copying code between pages.
In our Next.js 13 app, we can find the header component rendered in the root layout. We can modify the component to be an async function and fetch the user data using the `getServerSideProps` function. By logging the user data, we can see it rendering in the terminal.
Now, we can destructure the name from the user data and display it in the header component. With this approach, both the home page and the about page will fetch and render the data without any changes to the individual pages. This is made possible by the power of nested layouts and server components in Next.js 13.
Token Performance Metrics
Next.js 13 also introduces token performance metrics, which allow developers to measure the performance of their applications. These metrics provide valuable insights into the performance of data fetching and rendering processes, helping developers optimize their code and improve user experience.
Frequently Asked Questions (FAQs)
Q: Can I still use the Next.js 12 approach in Next.js 13?
A: Yes, you can still use the Next.js 12 approach of fetching data in the underscore app in Next.js 13. However, the new approach of fetching data inside components offers a more efficient and flexible way of handling data fetching.
Q: Are there any performance benefits to using Next.js 13 for data fetching?
A: Yes, Next.js 13 introduces server components and token performance metrics, which can significantly improve the performance of data fetching. Server components allow for more efficient rendering and sharing of data between components, while token performance metrics provide insights into the performance of data fetching processes.
Q: Can I migrate my Next.js 12 app to Next.js 13 easily?
A: Migrating a Next.js 12 app to Next.js 13 should be relatively straightforward. However, it is recommended to thoroughly test the app after migration to ensure that all components and data fetching processes work as expected. The new features and improvements in Next.js 13 can greatly enhance the performance and flexibility of your app.
Conclusion
Data fetching is a crucial aspect of web development, and Next.js provides powerful tools and features to simplify the process. In this article, we explored the differences between data fetching in Next.js 12 and Next.js 13. We learned how Next.js 13 introduces server components and token performance metrics, which enhance the performance and flexibility of data fetching. By fetching data directly inside components, developers can create more efficient and maintainable applications.
Does the Header component refetch the user while navigating client side?
Could you do a video on how to do this while using client side pages which most pages are? only components are serverside right?
Quick question: what shortcut did you hit at 1:26 to bring up the import dialog?
I see Sam selikoff 🤝🏾 I watch
Is this really SSR? I think no! It will be static page with fetched data from build time… Try to deploy your app, open your newly deployed website, update your backend data and refresh your website, the new into will not appear.
Awesome stuff, can't wait for Typescript to allow the usage of Async components without needing to use an ignore line flag
How to send data from server components to client components
How to get cookies from the layout component?
This man said fuck a Intro and got to the point. we need more vids from him.
Great
sorry not a fan of next 13, next 12 was great
Kinda a bad example at the start since the header would practically be inside of the layout.
Does anybobdy knows why in your video you can make your component async and me I got this error whenever I try ? "index.js?46cb:602 Uncaught Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead."
at 2:30 you said that it will not log because it's a server component, but what about at 0:44? I thought every component in pages directory are server side rendered. Why is it being able to log the user data into the client?
These videos teach me more than reading multiple pages of the docs. At least that's my experience. I'd love to see git repos for the videos, in this one, i am particularly interested in the nav link component
I notice you did not go back to the Layout component after you converted Header to async. You would have seen a nasty TypeScript error complaining that the component is not valid (I'm sure you know this).
I hope Vercel is really on top of getting that fixed. A little disappointed you did not show that error, as it makes it look like life is completely good and simple (which it is not with NextJS 13 at the moment)
This came in clutch when migrating to the 13 beta; thank you thank you 🙏🏻
Great format, would love more content in this straight-to-the-point way
You used the getUser function, not fetch which can be provided with auto and demanded data validation. I am now struggling to get it to work with fetch… and with no luck.
Sam's webDev god
Thanks man ❤❤❤❤
From Rick Grimes the Sheriff to Rick Grimes the Programmer
i am not use "use client" data fetching in next 13
Is it mandatory for sever side rendering creating app directoy?
Can I render serverside inside src directory?
but there's a little issue with next 13, when you use async on a page you can't make client side actions on that page or any of its parent pages… do you have any help with this?
How to do server side pagination by just fetching data from an API (without using MongoDB )?
Please help! I'm stuck in the middle of a project.
What if I'll use "useState"?? because for that I'll have to add "'use client'", and after that the page will be rendering in the client side… How to solve that problem???
How can I fetch server-side data and use useState in one app? Just like pages router used to do it