Routing in Next.js
How file-based routing works in the App Router.
Routing in Next.js
Next.js uses file-based routing. Every folder inside app/ becomes a URL segment, and a page.js file inside it makes that route visible.
app/
page.js -> /
about/page.js -> /about
blog/[slug]/page.js -> /blog/:slug
Dynamic Routes
Wrap a folder name in square brackets to capture a URL parameter.
// app/blog/[slug]/page.js
export default async function BlogPost({ params }) {
const { slug } = await params;
return <h1>Post: {slug}</h1>;
}
Linking Between Pages
import Link from "next/link";
export default function Nav() {
return <Link href="/about">About</Link>;
}
Link enables client-side navigation, so pages feel instant after the first load.
Advertisement728 x 90
Common Errors & Troubleshooting
- 404 on a route you're sure you created — the folder structure has to match exactly, and the file must be
page.js— a stray typo likepages.jsor putting it directly inapp/blog.jsinstead ofapp/blog/page.jswill silently 404. paramsis a Promise, not a plain object — in recent Next.js versions,paramsmust beawaited before you can readparams.slug— forgetting theawaitgives you a Promise object instead of the actual value.<a>causes a full page reload instead of client-side navigation — make sure you're importingLinkfromnext/linkand using it instead of a plain HTML<a>tag for internal links.
Advertisement320 x 100