Get AI summaries of any video or article — Sign up free
Next.js in 100 Seconds // Plus Full Beginner's Tutorial thumbnail

Next.js in 100 Seconds // Plus Full Beginner's Tutorial

Fireship·
5 min read

Based on Fireship's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Next.js mitigates client-side rendering drawbacks by serving fully rendered HTML to crawlers and users before React hydration.

Briefing

Next.js is positioned as a React framework that fixes two major weaknesses of client-side rendering—poor search/social indexing and slower first content—by letting pages arrive as fully rendered HTML from the server, then switching to interactive React in the browser. That “best of both worlds” approach matters because search engines and link-bot crawlers depend on HTML content being present immediately, while users still need fast, interactive behavior once the page loads.

In a Next.js project, routing is driven by the filesystem. A top-level `pages` directory maps directly to URLs: each JavaScript file exports a default React component that represents a route. For example, creating `pages/hello.js` makes `/hello` work automatically. Navigation is handled by Next’s router, so moving between routes behaves like a typical React app without manual wiring.

Dynamic routing extends this model. By creating a folder like `pages/cars` and adding an `index.js` for the list view, Next can render nested routes. Individual car pages use bracket syntax—`[id].js`—so `/cars/tesla` or `/cars/whatever` loads the same component with different URL parameters. Inside that dynamic page, the `useRouter` hook reads query parameters (such as `id`) and renders them into the UI.

Next also includes an `api` directory for server-only endpoints. Code placed there runs on the backend and doesn’t inflate the client-side JavaScript bundle, making it useful for server work or for exposing APIs.

The core differentiator is data fetching and server-rendering strategies. Next supports three approaches from the same codebase: static generation (pre-rendering), server-side rendering (SSR), and incremental static regeneration (ISR). Static generation uses `getStaticProps` to fetch data at build time, then bakes the resulting HTML into files that can be cached aggressively via a CDN—ideal for blogs or content that changes infrequently. The trade-off is staleness: updates require rebuilding and redeploying.

For dynamic routes under static generation, Next needs to know which pages to pre-render ahead of time. That’s handled by `getStaticPaths`, which returns a list of all route parameters (e.g., `tesla`, `ford`, `lambo`) and can include fallback behavior. With the fetched data, pages can render SEO-critical elements like titles and social meta tags using Next’s `Head` component.

When data changes constantly, SSR uses `getServerSideProps`, which runs on every request so the server fetches the latest data each time. SSR is more accurate but less efficient because it requires ongoing server computation rather than relying on globally cached static assets—an approach likened to high-churn marketplaces.

Finally, ISR offers a middle path: adding a `revalidate` option to `getStaticProps` allows Next to regenerate pages when requests arrive after a specified interval, reducing staleness without the full cost of SSR. The result is a flexible rendering toolkit that can be applied per page rather than forcing a single strategy across an entire app.

Cornell Notes

Next.js improves on client-side React by delivering fully rendered HTML to users and crawlers before React hydration kicks in. Routing is file-based under the `pages` directory: each file exports a default component for a URL, and bracket folders like `[id].js` enable dynamic routes. Data fetching determines how pages are rendered: `getStaticProps` pre-renders at build time (fast and CDN-friendly but can go stale), `getServerSideProps` renders on every request (fresh but server-heavy), and ISR uses `revalidate` to refresh static pages on a schedule. For dynamic static generation, `getStaticPaths` must list all parameter values to pre-render. These options let developers balance SEO, performance, and data freshness per route.

Why does server-rendered HTML matter for SEO and social sharing compared with client-side rendering?

Client-side rendering often sends an initial HTML “shell” with little or no content, then fetches JavaScript to render the page in the browser. That can leave search engines and social link bots without the final content to index or preview. Next.js addresses this by rendering the full HTML on the server first, so crawlers see titles and page content immediately, while users still get interactive React behavior after hydration.

How does Next.js map URLs to code, and what does “default export” mean in this setup?

Next.js uses the `pages` directory as the routing table. A file like `pages/hello.js` automatically becomes the `/hello` route. Each route file must export a default React component, which Next uses as the page’s content. This filesystem-to-URL mapping also means the folder structure mirrors the actual URLs users navigate to.

How do dynamic routes work in Next.js for URLs like `/cars/tesla`?

Dynamic routes use bracket syntax in filenames. For example, `pages/cars/[id].js` matches any `/cars/<value>` path. The `useRouter` hook reads the `id` parameter from the URL query, and the component renders that value (and related data) into the page.

What’s the difference between `getStaticProps`, `getServerSideProps`, and ISR’s `revalidate`?

`getStaticProps` runs at build time to pre-render pages, producing fast, CDN-cacheable HTML but risking stale data until a rebuild. `getServerSideProps` runs at request time, fetching fresh data for every user request but requiring server capacity and reducing caching efficiency. ISR keeps `getStaticProps` but adds `revalidate` so Next regenerates pages when requests arrive after a time interval, balancing freshness and performance.

Why does `getStaticPaths` matter for static generation with dynamic routes?

Static generation needs to know which dynamic pages to pre-render ahead of time. `getStaticPaths` returns a `paths` array listing every route parameter combination (e.g., `tesla`, `ford`, `lambo`). Without it, Next can’t determine which `[id]` pages to build during the pre-render step.

How does Next.js support SEO metadata in these server-rendered pages?

Next’s `Head` component lets pages inject SEO-relevant tags into the document head during rendering. In the car example, the title can include data like the car color and id, and meta tags for Twitter and Facebook cards can be added so social previews reflect the rendered content.

Review Questions

  1. When would `getStaticProps` be a better choice than `getServerSideProps`, and what failure mode (staleness) should teams plan for?
  2. For a dynamic route like `pages/cars/[id].js`, what roles do `getStaticPaths` and `useRouter` play, and how do they interact?
  3. How does ISR reduce the trade-off between CDN caching and data freshness, and what does `revalidate` control?

Key Points

  1. 1

    Next.js mitigates client-side rendering drawbacks by serving fully rendered HTML to crawlers and users before React hydration.

  2. 2

    File-based routing under `pages` maps directly to URLs; each route file must export a default React component.

  3. 3

    Dynamic routes use bracket syntax like `[id].js`, and `useRouter` reads URL parameters to drive page rendering.

  4. 4

    Static generation (`getStaticProps`) pre-renders at build time for fast CDN delivery, but data can become stale without redeploys.

  5. 5

    Server-side rendering (`getServerSideProps`) fetches fresh data per request, trading caching efficiency for up-to-date content.

  6. 6

    Dynamic static generation requires `getStaticPaths` to enumerate all parameter values to pre-render ahead of time.

  7. 7

    Incremental static regeneration (ISR) uses `revalidate` to refresh static pages on a schedule, offering a middle ground between speed and freshness.

Highlights

Next.js delivers server-rendered HTML first, improving how search engines and social link bots see content while keeping React interactivity for users.
Dynamic routes like `pages/cars/[id].js` combine filesystem routing with `useRouter` to render different pages from the same component.
`getStaticProps` + `getStaticPaths` enable pre-rendering dynamic pages, but ISR’s `revalidate` is the escape hatch when data changes over time.
SSR (`getServerSideProps`) guarantees latest data but requires server capacity and reduces the benefits of global CDN caching.

Topics

  • Next.js Rendering
  • Server-Side Rendering
  • Static Generation
  • Dynamic Routing
  • Incremental Static Regeneration