code q&a
Resources About
Question:

What is Next.js?

Next.js is a framework for building websites with React. It adds a few big things on top of React. It turns your folders into pages, builds pages on the server and lets you write backend API routes in the same project.

A plain React app runs in the browser and builds the page there with JavaScript. A page built like that is client-side rendered and an app built that way is usually a single-page app. Next.js can build the HTML on the server first so the page shows up fast and search engines can read it. That’s server-side rendering, or SSR.

Routing works through folders. A file at app/about/page.js becomes the /about page:

export default function About() {
  return <h1>About us</h1>;
}

Vercel makes Next.js and gives it away free as open source. Guillermo Rauch co-founded the company, which was called ZEIT when Next.js first came out in 2016. Vercel is the easiest place to deploy a Next.js app but you can host one anywhere that runs Node.js.

Server-side rendering with React comes with its own headaches. The page gets built on the server and then React runs again in the browser to make it interactive, which is called hydration. You get hydration errors that can be hard to track down if the HTML from the server doesn’t match what the browser builds. Your code also has to work in both places, so anything that touches the browser, like window or local storage, breaks on the server unless you guard it. Next.js also splits components into server components and client components. Figuring out which code runs where takes some getting used to.

I remember when Next started blowing up and it looked like it was going to take over. I played around with it but never used it for a professional project. React SSR always seemed to me like it had a lot of complications. I’d probably use Rails instead if I really cared about SSR. A lot of people still use Next.js and it’s the most popular React framework. It just never took over everything the way it seemed like it would.

You might also like