Next.js recently released their new App Router, which introduces an entirely new paradigm for creating Next.js apps. As part of Virtual Coffee's Lunch & Learn series, I gave a two-part presentation where we dove in to how the App Router works.
In Part 1, we cover some JS history, and then dive in to the new App Router. We also spent some time on React Server Components.
In Part 2, we take a look at data fetching in the App Router, and ways Next.js can help optimize your app with little work on your end.
You'll find some value here whether you're experienced with Next.js already or have never touched it - it's a great tool and the ways that it hooks in to both the React ecosystem and the Javascript/Typescript ecosystem as a whole offers a lot to learn.
I created a companion GitHub repository that contains examples. The talk is broken up into sections - and each section has a corresponding Pull Request.
The main
branch has the full thing, each Pull Request is based off of the other, starting with the Base Install.
See the full running app here on Vercel
Resources
- Next.js Docs
- App Router Playground
- Next.js on React Server Components
- React Server Components on patterns.dev
- Hacker News thread by Dan Abramov
Talk Sections:
0: Base install
(branch only, no PR)
We start with the output of npx create-next-app@latest
, with a couple small adjustments
- Update design
- Use
pnpm
1: Intro, Routes and Layouts
- Add some basic routes
- Add a Root Layout
- Add some nested layouts
- Add some examples of Client Components
2: Route Groups and Dynamic Routes
- Add a Route Group
- Add a Dynamic Route
- Add Dynamic Metadata
3: Handling Errors
- Add error handlers
- Add 404 handlers
4: Data Fetching
- Add Data Fetching to Route
- Add Data Fetching to Layout
- Add
loading.js
files
5: Suspense
- Add
Suspense
example
6: Rendering and Caching
- Data
fetch
on static route - Data
fetch
on dynamic route -
fetch
withcache: no-store
-
fetch
withrevalidate: 0
-
fetch
withrevalidate: 5
-
fetch
with a Static page +revalidate = true
Route Segment Config