#get nextjs slug from middleware
1 messages · Page 1 of 1 (latest)
Yes, it is possible to extract a slug (e.g., for a route like /example where you have a dynamic segment [example]) in Next.js middleware. However, the middleware does not automatically provide you with the dynamic route parameters (like slug) as props. Instead, you need to parse the URL yourself.
The recommended approach is to use the web standard URLPattern API to match and extract dynamic segments from the pathname in your middleware. Here’s an example based on the official documentation:
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
const pattern = new URLPattern({ pathname: '/:slug' })
export function middleware(request: NextRequest) {
const match = pattern.exec(request.nextUrl.pathname)
if (match && match.pathname.groups.slug) {
const slug = match.pathname.groups.slug
// You can now use the slug variable as needed
// For example, log it or use it in a rewrite/redirect
console.log('Slug:', slug)
}
return NextResponse.next()
}
This approach allows you to extract the slug (or any other dynamic segment) from the URL in middleware, regardless of how your dynamic routes are defined in your app. This is the recommended way after the deprecation of the old page match data in middleware see: Middleware Upgrade Guide.
If you have more complex routes (e.g., nested segments), you can adjust the URLPattern accordingly, such as /:parent/:slug or similar.
Summary:
You must manually match and extract the slug from the URL in middleware using URLPattern, as dynamic params are not automatically provided in middleware anymore Middleware Upgrade Guide.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@lusty wraith Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. Give me one moment! :books:
I'm sorry, but the knowledge sources do not provide a built-in way in Next.js middleware to distinguish whether a pathname like /example1 is matched by a static route or by a dynamic slug (e.g., [example]). Middleware does not have access to the route manifest or information about which paths are statically generated versus dynamic at runtime.
The recommended approach is to use the URLPattern API to extract potential slugs from the pathname, but this only tells you that the path matches a pattern (e.g., /:slug), not whether it is a static or dynamic route. The middleware operates before the route is resolved, so it cannot differentiate between static and dynamic matches at this stage No More Page Match Data.
If you need to know whether a path is static or dynamic, you would need to maintain a list of static paths yourself (for example, in a config or by importing a manifest generated at build time) and compare the current pathname against that list in your middleware. However, this approach is not covered in the official documentation and would require custom implementation.