-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A way to access the params and pathname in all server components #46618
Comments
I don't know if it is the same, but there is a similar issue : #43704 But the reasonning is that you can't access the pathname in Layouts as they don't rerender between navigations, so they can't be aware of the current pathname, same for searchParams. You can have access to Say you are on a route
With that i think you can reconstruct the current pathname. // middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// the following code is taken from : https://nextjs.org/docs/advanced-features/middleware#setting-headers
export function middleware(request: NextRequest) {
return NextResponse.next({
request: {
// New request headers
'x-pathname': request.nextUrl.pathname,
},
});
}
// the following code has been copied from https://nextjs.org/docs/advanced-features/middleware#matcher
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
}
// app/some/where/page.tsx
import { headers } from 'next/headers';
export default async function Page() {
const pathname = headers.get('x-pathname');
return <>{/*... your page code */}</>
} |
Ohh, I see. Somehow this completely escaped my mind. Yeah, in this case, there isn't much use for this feature anymore, as reused components like A different use case I thought of was some special element for specific pages inside the layout, which can be solved with route groups and additional layout files. I considered such approaches to be too much code, but now thinking back about it it's the only logical approach. Once again, thanks for the explanation! |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Describe the feature you'd like to request
Currently, inside server components, while it's possible to get the
params
in dynamic pages, and subsequently the pathname (params
+ hardcoding) in all pages, there isn't an easy way to access the information in all server components similar tousePathname
in client components.So sometimes I want to do something like this
but since I don't have
pathname
, I have to fallback to client components just to useusePathname
. While I'm fine with it so far for my use case, there are definitely cases where we want to use RSC instead (I don't want to sendSpecialButton
to pages that don't need it, for example).Describe the solution you'd like
A way to easily access the current pathname and params (and search params as well, maybe?) in any RSC.
There are several APIs I can imagine, but I don't know about their feasibilities:
Add a special prop like
_next
to all server components that contains the actualpathname
andparams
that the server component is rendered in. For TypeScript, we can have a typeNextServerComponent
Add a
getPathname()
andgetParams()
function to"next/navigation"
that can be used in server components.Describe alternatives you've considered
N/A
The text was updated successfully, but these errors were encountered: