What are API routes in Next.js and why would you use them?
API routes let you build backend endpoints directly inside your Next.js project, so you can handle things like form submissions or database calls without setting up a separate server.
The 50 Next.js questions worth preparing first, picked from all 200 on W3Teacher and spread across 40 topics. Each one has a short answer here and a full answer with examples and follow-ups on its own page.
API routes let you build backend endpoints directly inside your Next.js project, so you can handle things like form submissions or database calls without setting up a separate server.
Authentication confirms who a user is, usually through a login form or a third party sign in provider, while authorization decides what that authenticated user is allowed to do or see, such as whether they can access an admin page.
Code splitting breaks your JavaScript into smaller chunks that load only when they are needed, instead of sending the entire application code to the browser on the first page load.
Caching means storing the result of a data fetch or a rendered page so that future requests can reuse that saved result instead of doing the same work again.
A client component is a piece of your app that runs in the browser and can use things like state, click handlers, and browser only APIs.
The App Router gives you a cookies function from next/headers that you can call inside a server component to read any cookie sent with the request, letting you access things like a session token or a user's saved preferences without needing any client side code.
The custom App component, defined in pages/_app.js, wraps every page in your application, making it the perfect place to add global styles, shared layouts, or context providers that need to be available across the entire site.
You can simply use async and await directly inside a server component's function body, calling fetch or a database query, since server components run on the server and can handle asynchronous operations naturally before the page is sent to the browser.
You typically install a database client or an ORM library like Prisma, set up a connection string in your environment variables, and then call database queries directly inside server components, route handlers, or server actions, since these all run on the server where database…
You can deploy a Next.js app to Vercel, the platform built by the creators of Next.js which offers zero configuration deployment, to other hosting providers that support Node.js servers, or as a set of static files if your app does not need server side features, or using Docker…
Draft Mode lets content editors preview unpublished changes from a content management system before they go live, by temporarily bypassing the normal static caching so the page always shows the latest draft content instead of the cached published version.
A dynamic route lets a single page template handle many different URLs by using a folder name wrapped in square brackets, such as [id], which captures that part of the URL as a parameter you can use to fetch the correct data for that specific page.
The Node.js Runtime is the full Node.js environment with access to all Node.js APIs and packages, while the Edge Runtime is a lighter, faster environment that runs closer to your users around the world, but supports a smaller set of APIs and does not support every Node.js…
You create a file named .env.local in the root of your project and define key value pairs there, which Next.js automatically loads and makes available to your server side code through process.env, keeping sensitive values like API keys out of your actual source code.
You add a file named error.js inside a route folder, which must be a client component, and Next.js automatically shows it whenever an error occurs while rendering that route or any of its nested pages, letting you display a friendly message instead of a broken page.
You pass your server action function directly to the form element's action attribute, and Next.js automatically handles collecting the form data and sending it to your server action, even working correctly before any client side JavaScript has loaded, thanks to progressive…
The Image component automatically resizes images to fit the size they are displayed at, serves modern efficient formats like WebP when supported, lazy loads images that are not yet visible on screen, and prevents layout shift by reserving the correct amount of space before the…
Internationalization means preparing your application to support multiple languages and regions, such as translating text and formatting dates or currency differently depending on the visitor's locale.
A layout is a component defined in a layout.js file that wraps a page and any of its nested pages, letting you share common user interface elements like a navigation bar or sidebar across multiple pages without repeating that code in every single page file.
You add a file named loading.js inside the same route folder as your page, and Next.js automatically displays it while the page's server component is fetching data, giving users instant visual feedback instead of a blank screen while they wait.
You export a metadata object from your page or layout file, defining fields like title and description, and Next.js automatically generates the correct HTML head tags for search engines and social media platforms to read when they crawl or share your page.
Middleware is a special function defined in a middleware.js file at the root of your project that runs before a request completes, letting you inspect, modify, redirect, or block requests before they reach your actual pages or API routes.
You run the create-next-app command in your terminal, which walks you through a few setup questions like whether you want TypeScript or Tailwind CSS, and then generates a fully working project structure with all the necessary configuration files already in place.
Time based revalidation refreshes cached content automatically after a set number of seconds, while on-demand revalidation lets you manually trigger a cache refresh at the exact moment your content actually changes, such as right after publishing a new blog post, giving you…
getStaticProps is a special function you export from a page file that runs at build time, fetching data and passing it to your page component as props, allowing Next.js to generate a fully static HTML page ahead of time for fast loading and better SEO.
Parallel routes let you render two or more independent pages within the same layout at the same time, using named slots defined with an at sign prefix, which is useful for building dashboards where different sections, like analytics and recent activity, load and update…
You add a redirects function inside your next.config.js file, returning an array of redirect rules that each specify a source path, a destination path, and whether it should be a permanent redirect, which Next.js applies automatically without needing any additional code.
Server side rendering, called SSR, generates the page fresh on every request. Static site generation, called SSG, generates the page once at build time and reuses it for every visitor.
A route handler is a file named route.js placed inside a folder within your app directory, exporting functions named after HTTP methods like GET or POST, letting you build backend API endpoints directly within the App Router, similar to what API routes did in the Pages Router.
In the App Router, every folder inside the app directory represents a segment of the URL path, and adding a file named page.js inside that folder makes it a visitable route, so the folder structure of your project directly mirrors the URL structure of your website.
Anything included in a client component becomes part of the JavaScript bundle sent to every visitor's browser, meaning anyone can view it by inspecting the page, so secrets like API keys or database credentials must stay in server only code, such as server components, route…
A server action is a function marked with a use server directive that runs only on the server but can be called directly from a client component or a form, letting you update data, such as saving a form submission, without needing to manually create a separate API route just to…
A server component renders entirely on the server and sends only the resulting HTML and minimal necessary data to the browser, without shipping its own JavaScript to the client, which is why it is the default in the App Router, keeping applications fast and lightweight unless…
Local component state, managed with the useState hook inside a client component, is enough when a piece of data only matters to that one component and its direct children, such as whether a dropdown menu is currently open, without needing to be shared more broadly across the…
CSS Modules let you write regular CSS in a file named with a .module.css extension, and Next.js automatically scopes those class names so they only apply to the component that imports them, preventing styles from accidentally leaking into or being overridden by other components…
Unit tests check individual functions or small components in isolation, integration tests check how several pieces work together such as a form and its validation logic, and end to end tests simulate a real user clicking through your actual application in a browser to verify…
The next/script component gives you fine grained control over when and how third party scripts load, letting you choose strategies that avoid blocking the main page content from rendering, unlike a plain script tag which can slow down the initial page load if placed carelessly.
Turbopack is a fast bundler built specifically for JavaScript and TypeScript applications, designed to replace the older bundling tools used by Next.js, offering significantly faster startup times and quicker updates while you are actively developing, since it only rebuilds the…
You can choose TypeScript when running create-next-app, or add it to an existing JavaScript project by installing TypeScript and its type definitions and renaming your files to use .ts or .tsx extensions, after which Next.js automatically detects and creates the necessary…
Core Web Vitals are a set of specific metrics that measure real user experience, including how quickly the main content loads, how quickly the page responds to the first user interaction, and how much the layout shifts unexpectedly while loading, and they matter because search…
In the Pages Router, the request object passed to your handler function gives you access to query parameters through req.query and to the submitted data through req.body, letting you build endpoints that respond differently based on what the client sent.
Hydration is the process where React takes the static HTML that was already sent from the server and attaches interactivity to it in the browser, such as making buttons clickable and inputs responsive, without having to rebuild the entire page from scratch.
The custom Document component, defined in pages/_document.js, controls the initial HTML structure of your page, such as the html and body tags, and is the right place to add things like custom fonts, a language attribute, or third party scripts that need to load in the head or…
Since client components cannot use async directly in their function body the way server components can, you typically fetch data inside a useEffect hook, storing the result in state with useState, and showing a loading indicator while the request is in progress.
You check the user's session at the top of a server component or in middleware, and redirect them to the login page immediately if no valid session is found, before any of the protected content is rendered or sent to the browser.
The next/dynamic function lets you import a component only when it is actually needed, such as when a modal opens, keeping that component's code out of the main bundle until the moment it is required, which reduces the initial page weight.
Next.js caches at several layers, including the fetch request cache which stores data fetching results, the full route cache which stores rendered pages, the router cache on the client which stores recently visited pages for instant back navigation, and the data cache for…
You typically set cookies from a server action or a route handler using the cookies function, calling its set method with the cookie name, value, and options like httpOnly and secure flags, which controls how the browser stores and sends that cookie on future requests.
Database credentials and connection strings must remain secret, and browsers cannot be trusted to keep secrets safe since anyone can view the JavaScript running on their machine.
You run the build command to generate an optimized production build, then use the start command to run a Node.js server that serves that build, making sure any required environment variables are set on the server before starting it.
Run a timed Next.js mock interview: random questions, answer out loud, reveal the model answer and get a readiness score you can share.