App router: How to get cookie in the layout file without "use client"? #69764
-
|
This is for Next.js 14 / 15, using React Server Components, not Server Side Rendering. I want to read a cookie for all pages and so I'm trying to do it in the main Context: I'm working on a multi-tenant app, and we store the current org id in a cookie. We want to have that org id hydrated and immediately available to our components on the client side. |
Beta Was this translation helpful? Give feedback.
Answered by
theogravity
Sep 5, 2024
Replies: 1 comment
-
|
Ended up discovering https://github.com/shahradelahi/next-extra for the fix: From the repo example: import { pathname, searchParams } from 'next-extra/pathname';
export default async function Layout({
children,
}: Readonly<{ children: React.ReactNode }>) {
// Assuming a request to "/hello?name=John"
const route = pathname(); // /hello
const params = searchParams(); // ReadonlyURLSearchParams { 'name' => 'John' }
return children;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
theogravity
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ended up discovering https://github.com/shahradelahi/next-extra for the fix:
From the repo example: