Next.js 13: Data Fetching Strategies

Rate this content
Bookmark
Github

- Introduction

- Prerequisites for the workshop

- Fetching strategies: fundamentals

- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)

- Test your build and serve it on Vercel

- Future: Server components VS Client components

- Workshop easter egg (unrelated to the topic, calling out accessibility)

- Wrapping up

Alice De Mauro
Alice De Mauro
53 min
12 Dec, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This workshop at React Day Berlin covered fetching strategies for the new App Directory, including server components, client components, and dynamic fetching. Next.js introduced powerful fetching strategies like ISR and on-demand ISR, along with other enhancements in Next 13. The workshop emphasized the importance of setting up a development environment and creating a production build for testing. It also discussed the benefits of using caching and revalidating strategies in Next.js for efficient data fetching. The workshop concluded with a focus on streaming with suspense for independent streaming of UI components and enhancing interactivity for users.

1. Introduction to Workshop and Fetching Strategies

Short description:

Welcome to our workshop, React Day Berlin. I'm Alice De Mauro, a sales engineer at Vercell. We'll cover fetching strategies for the new App Directory, including server components, client components, and dynamic fetching.

Hello, everybody. Welcome to our workshop, React Day Berlin, about 9CX13 and data fetching especially for what concerns the new app directory. I'm Alice De Mauro, I'm a sales engineer at Vercell, and a sales engineer means basically that I am a solution architect for pre-sales.

So basically, I check the architecture of our customers and I see if Vercell is a fee. What is Vercell? If you don't know it, we created and are maintaining SGS. Webpack, we have people working from Webpack and we just released TurboPack. We do also have in-house TurboRepo and other tools that might be useful for developers, in general, if you don't know us, we are the creator of the legendary Next.js, which is the framework for React.

I'm going to immediately start with what are our prerequisite basically or what at least I'm going to use for you workshop. Something that I'm going to do pretty often is this movement here. I hope it's not too jarring, but I'm really used to go for my environment like that. What do I have? I have my ID. I have my terminal and my Visual Studio code. I do already have some code prepared. The code is going to be at this place.

I'm going to actually give you two links. One is going to be the clean code before the workshop basically. I'm going to drop it in the chat. This is public and you can just access it. I simply forked one of our famous repositories for the playground for Next.js app and just tweaked it for this workshop specifically. What I'm going to do is to actually use the pull request to welcome to our workshop in order to paste or to deploy our changes. All the changes I'm going to make, you're going to see them in this specific GitHub branch. What also I'm going to do, I have my Vercel dashboard. I created a ProTrial just for this workshop. Why? Because I needed it to have all preview comments enabled for you. If I go on my deployments, I have my preview. My preview is based on the welcome to the workshop branch. Basically, here I'm now going to paste this one. If you are logged in in Vercel, you got to paste it here. If you can login in Vercel, then you are able to actually comment on everything that is on this page. This is going to be our workshop baseline.

For example, I can do this example. I'm going to see your comments. If you don't have Vercel yet, consider that I had OBI just, I don't know, yesterday. I think yesterday I had OBI. It's super easy, you just continue with GitHub, you just login into GitHub and done. Same for GitHub and Bitbucket. Then you can comment on this specific URL. This URL will also leave after the workshop. Don't worry about eventually things that you forgot to ask or that you don't want to ask now because you want to check things further later on. Please just go here and comment. I'm going to receive them directly on here. Then the comments, they're going to see them on the Versal bot. I'm going to simply check them here. I think for what concerns what we're going to have, more or less, ready, it's done.

Something else. What is it all about? Our workshop will be about fetching strategies for the new App Directory. I'm going to show you server components, which is basically the default for the new Next.js. Client components and how to do it on SUR. Disclaimer is like you did it before, nothing different. Dynamic fetching, and how does it work when you opt-in, when you opt-out, and these kind of things.

2. Fetching Strategies and Server Components

Short description:

ISR and on-demand ISR are powerful fetching strategies. React Day Berlin just released Next 13, along with other enhancements. The Beta docs are available for feedback. Setting up a development environment and creating a production build are important for testing. The app directory in the new NestJS directory reflects the routes. Server components are always rendered first on the server and are fetched at build time, eliminating the need for round trips between client and server.

ISR, we provide that out of the box. It's pretty powerful. If you didn't try it, try it. On-demand ISR, powerful again, very, very powerful. That's probably the longest piece, because to set up on-demand ISR is a little bit more work, not too much, but still, but that's gonna be our most longest probably, and then how streaming works. So our suspense works in general, because that's from React, and how it will work together with Pagesy. These are all fetching strategy, can be like from the data perspective, they are tightly coupled with the page perspective, is also how the pages and the routes are rendered basically.

Just so you know, if you don't know Next, or you never used it, we just released Next 13. The app directory is not the only thing that we released, of course. There's gonna be also a lot of others, for example, TurboPack, or the image, the font, and so on, the scripts, all integrated and enhanced. So if you don't know, or you want to check more, just, you know, I pasted there the link. We also have the, I'm gonna fetch them right now, the Beta, this is specific to the fetching, data fetching strategy. But in general, we do have the Beta docs. If you are logged in in Vercel, you also can comment on them. So if you find something that is not clear, please give us feedback, you know, anything that is useful to enhance our docs, it's always good, especially if you're expecting something that's not there, please just don't be afraid to paste a comment there, you just click on these little icon and you can just comment. And we answer, so if there is some, you know, things that it's not clear on the docs, we do answer questions in general.

So back to the code. So I have my IDE here and I'm gonna have some code already done because of course, part of the fact that I'm very clumsy with my hands, so I tend to do a lot of typos, but apart of that, it's easier, it's faster, it's gonna be faster to actually paste the code from something that's already created, I'm gonna push this code anyway, so you're gonna have it on the GitHub account. First thing first, so, terminal, a nice terminal, two things that are very important. Of course, first thing first, you have to install everything, you're gonna use YARN. I usually use npm, while in my developer careers, I use npm all the time, but these days I don't know, I like YARN pretty much, so I'm using YARN for now, if you run YARN dev, so these are the two distinctive things that are very important. If you use YARN dev, you're gonna spin up the dev environment, which is the server-side rendering environment with hot reloading, so every time you change something, maybe super fast, and localhost, I'm gonna show you right now because I have localhost here, on localhost is gonna always show my changes right away whenever I do any change. Something that we're gonna use later on is really, really important to test all our fetching strategy, especially for what concerns the statically generated pages and the static fetching in general, is gonna be creating a production build. I'm gonna go right away to it because server components is one of these examples where if you have anything that you need to test from the static perspective, you will need to create a production environment and test there, because only there, you actually have the classic first build and then serve, right? For now I'm just gonna start right away. So I have my application here, the app directory is the new NestJS directory. I have everything in the package of JSON of course, and I do have some groupings. These groupings are not gonna be in your routing, they're just meant to tidy, to tie, sorry, your URL. So basically what I did was like, okay, I have a couple of fallbacks because it's of the loading pages and so on. Some youtube that I might or might not use, but what is important is our fetching. So from the root directory, localhost slash, we're gonna go through all these routing. Every single route is gonna be reflected from these names from the folders. This the same way a classic NestJS works. So server component is gonna be one of my routes and this is where I'm gonna start. I'm gonna go to my local host. So if I click on server components, which is the first one, you see server component is my, my route. So the name of the folder, exactly the name of the folder automatically done from next. Server components. So explanation of server components. They are running the server. They are gonna be always automatic. Pages and layouts are always, always server components. Always, they're gonna always be rendered first in the server. I will show you later on client components and, you know, how to divide the little pieces. By default they are steadily generated at build time. And that means that everything is gonna be fetched at the time you're gonna be fetched from the server. There is not gonna be any round trip from the client and the server. So there's not gonna be such a thing like, oh, I have the client, I have all my JavaScript and then I need the posts of my blog posts, right? And then I have to ask back and forth to the server. No give me this one, give me that one. So that's not gonna happen because everything is in the server, within the server and the server is gonna do all these kinds of fetchings without any round trip between client and server. And that's super, super fast because then they're already built.

Watch more workshops on topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
React Hooks Tips Only the Pros Know
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Simplifying Server Components
React Advanced Conference 2023React Advanced Conference 2023
27 min
Simplifying Server Components
Top Content
Server Components are arguably the biggest change to React since its initial release but many of us in the community have struggled to get a handle on them. In this talk we'll try to break down the different moving parts so that you have a good understanding of what's going on under the hood, and explore the line between React and the frameworks that are built upon it.
A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
Don't Solve Problems, Eliminate Them
React Advanced Conference 2021React Advanced Conference 2021
39 min
Don't Solve Problems, Eliminate Them
Top Content
Humans are natural problem solvers and we're good enough at it that we've survived over the centuries and become the dominant species of the planet. Because we're so good at it, we sometimes become problem seekers too–looking for problems we can solve. Those who most successfully accomplish their goals are the problem eliminators. Let's talk about the distinction between solving and eliminating problems with examples from inside and outside the coding world.
Using useEffect Effectively
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Can useEffect affect your codebase negatively? From fetching data to fighting with imperative APIs, side effects are one of the biggest sources of frustration in web app development. And let’s be honest, putting everything in useEffect hooks doesn’t help much. In this talk, we'll demystify the useEffect hook and get a better understanding of when (and when not) to use it, as well as discover how declarative effects can make effect management more maintainable in even the most complex React apps.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Too much JavaScript is getting you down? New frameworks promising no JavaScript look interesting, but you have an existing React application to maintain. What if Qwik React is your answer for faster applications startup and better user experience? Qwik React allows you to easily turn your React application into a collection of islands, which can be SSRed and delayed hydrated, and in some instances, hydration skipped altogether. And all of this in an incremental way without a rewrite.
Routing in React 18 and Beyond
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Concurrent React and Server Components are changing the way we think about routing, rendering, and fetching in web applications. Next.js recently shared part of its vision to help developers adopt these new React features and take advantage of the benefits they unlock.In this talk, we’ll explore the past, present and future of routing in front-end applications and discuss how new features in React and Next.js can help us architect more performant and feature-rich applications.