Parsing Next.js pathname to structured segments, which you can use in various ways.
npm install next-router-segmentsI've made this package for parsing Next.js pathname to structured segments, which you can use in various ways, for
example for creating breadcrumbs for current page.
``bash`
pnpm add next-router-segments
`bash`
npm i next-router-segments
`bash`
yarn add next-router-segments
`tsx
"use client";
import usePathname from "next/navigation";
import { getRouteSegments } from 'next-router-segments';
export default function Page() {
const {pathname} = usePathname();
const segments = getRouteSegments(pathname)
return (
{JSON.stringify(segments, null, 2)}
Info: pathname is only available in client, so usage for client component and client pages is exacly same.
$3
`tsx
import useRouter from "next/router";
import { getRouteSegments } from 'next-router-segments';export default function Page() {
const {pathname} = useRouter();
const segments = getRouteSegments(pathname)
return (
{JSON.stringify(segments, null, 2)}
)
}
``