How to Get URL pathname in Next.js?
Last Updated :
18 Jul, 2024
Next.js is a powerful React framework that simplifies the process of building server-rendered and statically generated web applications.
One common requirement in web development is to get the URL pathname, which can be useful for various purposes such as conditional rendering, routing, and analytics. In this article, we will explore different methods to get the URL pathname in a Next.js application.
Prerequisites:
What is an URL pathname?
The URL pathname is the part of the URL that comes after the domain name, including the initial slash. For example, in the URL https://geeksforgeeks.org/about
, the pathname is /about
. Knowing how to retrieve the pathname can help in various cases, such as displaying different content based on the current route or logging user activity.
Steps to Get URL pathname in Next.js
We will be using pnpm as the package manager and TypeScript, though you can use npm and JavaScript without any difference.
Step 1: Create a next.js project using the following command.
Navigate to desired directory and type the following command in terminal
pnpm create next-app@latest
Next.js Project SetupFolder structure

Get URL pathname in Pages router
In pages router, useRouter() hook is used to get pathname and slug of dynamic routes.
Example 1: Getting the pathname
JavaScript
//pages/pages-router/index.tsx
import { useRouter } from "next/router";
export default function PagesRouter() {
const router = useRouter();
return <div>Pages Router: {router.pathname}</div>;
}
Output

Example 2: Getting slug of a dynamic route.
A dynamic route is created by wrapping in square brackets: [segmentName]. For example [id], [slug]
JavaScript
//pages/pages-router/[slug]/index.tsx
import { useRouter } from "next/router";
export default function PagesRouter() {
const router = useRouter();
return <div>Pages Router Slug: {router.query.slug}</div>;
}
Output

Example 3: Getting the whole pathname of a dynamic Router
JavaScript
//pages/pages-router/[slug]/index.tsx
import { useRouter } from "next/router";
export default function PagesRouter() {
const router = useRouter();
return <div>Dynamic page: {router.asPath}</div>;
}
Output

Get URL pathname in App router
Client Component
In client component usePathname() hook is used to get pathname.
JavaScript
"use client";
import { usePathname } from "next/navigation";
const Page = () => {
const pathname = usePathname();
return <div>Current path: {pathname}</div>;
};
export default Page;
Output

Server Component
Reading the current URL from a Server Component is not supported. This design is intentional to support layout state being preserved across page navigations. As server component renders on the server side, there is no window object from which we can get URL pathname. Other hacky solutions like next/headers don't work in Next 14 and is not recommended to use as it causes severe performance issues. If your logic need pathname then consider it moving to client component.
Conclusion
In this article we have learned how to setup what are URL pathname, how to setup Next.js project, how to get pathname in pages router and app router.
Similar Reads
How to Get Query Parameters from URL in Next.js?
In Next.js, getting query parameters from the URL involves extracting key-value pairs from the URL string to access specific data or parameters associated with a web page or application, aiding in dynamic content generation and customization. To get query parameters from the URL in next.js we have m
5 min read
How to Get The Current URL Domain in Next.js ?
In Next.js, we can get current url domain with the help of built-in methods like window and document location. In this article, we'll explore different approaches and best practices for obtaining the current URL domain in Next.js applications. The approaches to get the URL of the current domain in n
3 min read
How to add Web Share in Next.js ?
The Web Share API enables web applications to share content (like URLs, text, or files) to other apps installed on a user's device, such as social media platforms, messaging apps, or email clients. Integrating the Web Share API into a Next.js project enhances user experience by providing a seamless
2 min read
How To Get Current Route In Next.js?
Next.js is a popular React framework that makes it easy to build server-side rendered and static web applications. One common task in web development is determining the current route or URL of the page. In Next.js, this can be done using the built-in useRouter hook. This article will guide you throu
3 min read
Node.js URL.pathname API
The url.pathname is an inbuilt application programming interface of class URL with in url module which is used to get and set the pathname portion of the URL. Syntax: const url.pathname Return value: It gets and sets the pathname portion of the URL. Below programs illustrate the use of url.pathname
1 min read
How to change port in Next.js App
Next.js provides the flexibility to change the default port of the development server using command-line options such as -p <port_number> or by updating the "scripts" section in the package.json file, offering developers multiple ways to customize the port configuration based on their requirem
3 min read
How to Share Data Between Pages in Next.js?
Sharing data between pages in Next.js can be crucial for maintaining state and passing information efficiently. You can achieve this with the help of URL parameters. In this article, we will learn about how to share data between pages in next.js Approach to Share Data Between PagesUse the Link compo
2 min read
How to add Video Player in Next.js ?
Adding a video player in a Next.js application enhances the user experience by enabling video playback. The react-player library simplifies integrating various video sources such as YouTube, Vimeo, and local files. In this article, we are going to learn how we can add Video Player in NextJS. Approa
2 min read
How to Change URL Without Page Refresh in Next.js?
In web development, the ability to change URLs dynamically without triggering a full page refresh is essential for creating smooth, seamless user experiences. Next.js, a popular React framework, offers built-in features to achieve this efficiently through its client-side routing capabilities. In thi
3 min read
How to use get parameter in Express.js ?
Express Js is a web application framework on top of Node.js web server functionality that reduces the complexity of creating a web server. Express provides routing services i.e., how an application endpoint responds based on the requested route and the HTTP request method (GET, POST, PUT, DELETE, UP
2 min read