Page couldn't be rendered statically because it used `cookies` (supabase)
I got this error during build process
codesandbox - https://codesandbox.io/p/github/nicitaacom/auth-cookie-supabase-willbedeleted/auth-form-next
Here is my files that use cookies
app/(auth)/auth/callback/route.ts
middleware.ts
app/utils/supabaseServer.ts
`
How fix this error?
4 Replies
Cookies are sent by the browser when the page is requested. So, you can't refer to them outside of a context that's handling a request (like in a utility function).
You need to have the Supabase connection stuff in the actual component that's being rendered. The docs are very extensive, and I'd think you can just model your code after them.
Supabase Auth with the Next.js App Router | Supabase Docs
Authentication and Authorization helpers for creating an authenticated Supabase client with the Next.js 13 App Router.
It means every time I want to use supabase in server component I need to write this?
That was my read of the documentation. Each Supabase connection is for a particular user.
I was actually curious if passing the
cookies
from next/header
straight into the create method, like you had in your previous code, was working for you.
cookies
returns a cookie store which is all that cookies: () => cookieStore
is doing, so I don't see why you need to get the cookie store only to essentially recreate the cookies
method with the result.
Arguably, extracting it and passing the result memoizes it so there's only one copy, but I'd think the overhead is trivial if, in fact, Next doesn't memoize internally.