Server Error: cookies called outside request scope in app router without i18n routing on dynamic routes #70499
Replies: 2 comments
-
Where you able to find an answer to this |
Beta Was this translation helpful? Give feedback.
0 replies
-
If you can pass your NextRequest or NextApiRequest to the function you can use it to work with cookies, you can view my custom solution to this bug for Supabase when using edge api routes... You can use req.cookies.getAll() import { createServerClient } from '@supabase/ssr'
import { NextRequest, NextResponse } from 'next/server'
export function createClient(req: NextRequest, res: NextResponse) {
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return req.cookies.getAll()
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value }) => req.cookies.set(name, value))
cookiesToSet.forEach(({ name, value, options }) =>
res.cookies.set(name, value, options)
)
},
},
}
)
return supabase
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
When using the app router in a Next.js 14 project without i18n routing (with next-intl for localization), I encountered a server error while accessing cookies in dynamic routes. The error message is:
Error:
cookieswas called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
This error occurs only in dynamic routes when attempting to retrieve cookies via the cookies() function from next/headers. Static pages work fine, but dynamic routes generate this error during page generation, indicating that cookies() is being called outside of the request context.
i18n.ts
Additional Context
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions