npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-pathname

v1.0.3

Published

Access request pathname in Next.js RSCs

Downloads

53

Readme

next-pathname

Access the request pathname in dynamically-rendered React Server Components.

Compatible with Next.js App Router 14 and 15.

Setup

Add the package to your Next.js App Router project:

npm i next-pathname

How it works

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.

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;
};

Accessing the URL pathname in RSC

Due to API changes between Next.js 14 and 15, the way to access the URL pathname in RSCs is slightly different.

  • For Next.js 14, you can call getPathname synchronously.
  • For Next.js 15, you must await getPathname before using the value.

Ensure you're using the correct import for your Next.js version.

  • For Next.js 14, import from "next-pathname/next14".
  • For Next.js 15, import from "next-pathname/next15".

Next 14.x

// 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>
  );
}

Next 15.x

// 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>
  );
}

Usage in 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.