Skip to content

Chapter 8 of 8

Configuration, Metadata, and Optimization

The main configuration file for a Next.js project is next.config.js, also available as .mjs or .ts. It centralizes many important settings, including allowed image domains, declarative redirects and rewrites, environment variable exposure, webpack customization, and experimental features. SEO is handled through metadata APIs: exporting a metadata object from layout.tsx or page.tsx sets static metadata such as the page title and description, while exporting an async generateMetadata function lets you fetch data dynamically and return metadata based on params or other inputs. Next.js automatically merges metadata from parent layouts with metadata defined deeper in the tree. For dynamic routes that should be statically generated at build time, the generateStaticParams function returns the list of parameter combinations to pre-render.

Images are optimized through the built-in <Image> component from next/image. It handles lazy loading by default, generates responsive sizes automatically, converts images to modern formats like WebP and AVIF, and reserves layout space using the width and height props, or the fill prop when dimensions are unknown, to prevent layout shift. The priority prop disables lazy loading for above-the-fold images that should load immediately.

Environment variables are managed through files like .env.local. Variables without a prefix are only available on the server, which keeps secrets safe. To expose a variable to the browser, prefix it with NEXT_PUBLIC_, after which it becomes accessible in client code as well. For runtime selection, Next.js supports both the default Node.js runtime and the Edge runtime, which is a lightweight JavaScript environment optimized for low latency at CDN edge locations. Middleware and Route Handlers can opt into the Edge runtime with export const runtime = 'edge', trading some Node.js API support for faster cold starts.

Finally, the framework offers multiple ways to redirect users. Inside Server Components and Server Actions you can call redirect('/path') or permanentRedirect('/path'). In next.config.js you can declare redirects declaratively, while Middleware is the right tool for conditional redirects based on the request. After a mutation, calling revalidatePath('/blog') purges the cached data for that route, and revalidateTag('posts') invalidates every cached fetch tagged with 'posts', ensuring the next render reflects the most recent state without waiting for the next timed revalidation.

All chapters
  1. 1Foundations of Next.js and the App Router
  2. 2Server and Client Components
  3. 3Rendering Strategies
  4. 4Advanced Routing Patterns
  5. 5Layouts, Templates, and Special Files
  6. 6Data Fetching, Mutations, and Middleware
  7. 7Navigation Hooks and Components
  8. 8Configuration, Metadata, and Optimization

Drill it

Reading is not remembering. These come from the Nextjs Framework deck:

Q

What is Next.js?

Next.js is a React framework created by Vercel that provides features like server-side rendering, static site generation, file-based routing, and API routes out...

Q

What is the App Router in Next.js?

The App Router (introduced in Next.js 13) uses the app/ directory and supports React Server Components, nested layouts, streaming, and colocation of files. It r...

Q

How does file-based routing work in the App Router?

Routes are defined by the folder structure inside app/. A route is created by adding a page.tsx file inside a folder. For example, app/about/page.tsx maps to th...

Q

What is a Server Component in Next.js?

A Server Component renders on the server and sends HTML to the client. It is the default in the App Router. Server Components can directly access databases, rea...