
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
next-pathname
Advanced tools
Access the request pathname in dynamically-rendered React Server Components.
Compatible with Next.js App Router 14 and 15.
Add the package to your Next.js App Router project:
npm i next-pathname
This package provides a middleware wrapper that attaches the URL pathname to the incoming request object. This allows you to access the URL pathname in dynamically-rendered RSCs.
When middleware finishes running and the request is handed off to the lambda, the pathname is available on the headers object anywhere in the RSC environment.
This header is never exposed to the client because it is set after the request has been received by the middleware.
If you don't already have a middleware.ts
, create one and add the following code:
// src/middleware.ts
import { withPathname } from "next-pathname";
export const middleware = withPathname;
This will automatically attach the URL pathname to the request object.
For more complex or customized middleware, you can wrap the request object yourself.
Typically you'll want to include this after any other request handling you may already have in your middleware, i.e. for feature flags, rewrites, locale detection etc, but before any terminating handlers like next-intl
.
// src/middleware.ts
import { withPathname } from "next-pathname";
export const middleware = async (request: NextRequest) => {
// do some request preprocessing...
// attach the URL pathname to the request object
const modifiedRequest = withPathname(request);
// use modifiedRequest to produce a response
// example: using next-intl
const response = await i18nMiddleware(modifiedRequest);
return response;
};
Due to API changes between Next.js 14 and 15, the way to access the URL pathname in RSCs is slightly different.
getPathname
synchronously.getPathname
before using the value.Ensure you're using the correct import for your Next.js version.
"next-pathname/next14"
."next-pathname/next15"
.// src/app/my-route/page.tsx
import { getPathname } from "next-pathname/next14";
export default function Page() {
const pathname = getPathname();
return (
<div>
<h1>This segment was rendered on: {pathname}</h1>
</div>
);
}
// src/app/my-route/page.tsx
import { getPathname } from "next-pathname/next15";
export default async function Page() {
const pathname = await getPathname();
return (
<div>
<h1>This segment was rendered on: {pathname}</h1>
</div>
);
}
layout.tsx
Layouts in App Router are rendered once per route and do not re-render on the client. This causes getPathname
calls to return the same value for all pages that share the layout.
To work around this, you can use Next's usePathname
hook to access the URL pathname in the layout. You'll have to make a separate Client Component with 'use client'
to do this though.
FAQs
Access request pathname in Next.js RSCs
The npm package next-pathname receives a total of 7 weekly downloads. As such, next-pathname popularity was classified as not popular.
We found that next-pathname demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.