Skip to main content

Front-End Development

File Based Routing in Next.js

Next Js

Next.js is a framework for building web applications built on top of React. Everything we can do in React we can also do in Next.js – with some additional features like routing, API calls, authentication, and more. We don’t have these features in React. Instead, we must install some external libraries and dependencies – like React Router, for routing in a single-page React app.

But in Next.js, things are different. We don’t have to rely on external libraries to do these things. They come built into the package when we create a Next.js app. Next.js uses the file system to enable routing in the app.

Pages in Next.js

Pages in Next.js are React Components that are automatically available as routes. They are exported as default exports from the pages directory with supported file extensions like .js, .jsx, .ts, or .tsx. A typical Next.js app will have a Folder structure like the one below.

Picture1

Here, we have four pages:

Picture2

Linking and Navigating

By default, Next.js pre-renders every page to make your app fast and user-friendly.

There are two ways to navigate between routes:

  1. <Link> Component
  2. useRouter Hook

Link Component

<Link> is a React component that extends the HTML <a> element to provide prefetching and client-side navigation between routes. It is the primary way to navigate between routes in Next.js.

Picture3

Here, we have two routes:

The first link leads to the page http://localhost:3000/contact

The second link leads to the page http://localhost:3000/my-folder/about

useRouter() Hook

The useRouter hook allows you to programmatically change routes inside Client Components. To use useRouter, import it from next/navigation, and call the hook inside your Client Component. The useRouter provides push(), refresh(), and more methods.

Picture4

Types of Routes in Next.js

  • Dynamic Routes

When you don’t know the exact segment names ahead of time and want to create routes from dynamic data, you can use Dynamic Segments that are filled in at request time or prerendered at build time. Next.js enables you to define dynamic routes in your app using the brackets [params] instead of setting A static name on your pages. You can use a dynamic one.

Let’s consider the file structure:

Picture5

Next.js will get the route parameters passed in and then use it as a name for the route.

  • Nested Dynamic routes:

With Next.js, you can also nest dynamic routes with the brackets([params]). Let’s consider the file structure:

Picture6

Picture7

As you can see here, we set the dynamic values on the attribute as we did in the previous example. But this time, we must define the folder’s name and file.

  • Catch-all Segments

Dynamic Segments can be extended to catch-all subsequent segments by adding an ellipsis inside the brackets […folderName]. For example, pages/shop/ […slug].js will match /shop/clothes, but also /shop/clothes/tops, /shop/clothes/tops/t-shirts, and so on.

Picture8

Next.js will get the route parameters passed in and then use it as a name for the route. You can also get the route parameters with the useRouter hook on the client or getInitialProps on the server.

Picture9

Thoughts on “File Based Routing in Next.js”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Asif Khan

Asif Khan is a Senior Technical Consultant at Perficient. He is a Frontend Developer with Experience in Web Application Development. Asif is a Technical enthusiast who enjoys exploring new frameworks and technologies.

More from this Author

Follow Us