#Supabase cookies can only be modified in a server action, nextjs 15

1 messages · Page 1 of 1 (latest)

rich kraken
#

Recently upgraded to next 15, now my supabase ssr wont work without throwing this error when I try to get the currentUser

unhandledRejection: Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
at Proxy.callable (webpack://next/dist/src/server/web/spec-extension/adapters/request-cookies.ts:22:10)
at set (a:\AthleticScholar\AthleticScholarFrontend\lib\supabase\server.ts:16:87)
20 |
21 | public static callable() {

22 | throw new ReadonlyRequestCookiesError()
| ^
23 | }
24 | }

API Reference for the cookies function.

wraith crestBOT
#

Hi @rich kraken. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the knowledge sources, it appears that NextJS 15 has introduced some changes in how cookies can be modified, particularly when working with Supabase SSR. Let me explain the issue and provide some guidance:

In NextJS 15, cookies can only be modified in a Server Action or Route Handler. This is explicitly mentioned in the Next.js documentation (https://nextjs.org/docs/app/api-reference/functions/cookies#good-to-know):

HTTP does not allow setting cookies after streaming starts, so you must use .set in a Server Action (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations) or Route Handler (https://nextjs.org/docs/app/building-your-application/routing/route-handlers).

To resolve this issue with Supabase SSR, you need to ensure that you're initializing the Supabase client within a function scope, not in the global scope. This is because the cookies are not available outside of an HTTP request context.

Here's an example of how you can modify your code to work with NextJS 15 and Supabase:

"use server"

import { createClient } from "@/utils/supabase/server";

export async function getLatestProjects() {
  const supabase = createClient();
  let { data: projects, error } = await supabase
    .from('new_projects')
    .select('*')
    .order('end_date', { ascending: false })
    .limit(10);

  if (error) throw error;

  return projects;
}

This approach is mentioned in a GitHub issue comment (https://github.com/vercel/next.js/issues/67191) discussing a similar problem.

Remember to only initialize the Supabase client inside functions where it has access to cookies. This ensures that the client is created within the correct request scope.

#

If you're using this in a server component, make sure to call the function that initializes the Supabase client within an async function, as cookies is now an asynchronous function in NextJS 15 as per the documentation (https://nextjs.org/docs/app/api-reference/functions/cookies#good-to-know).

If you're still encountering issues, you might need to review how you're handling authentication and session management with Supabase in your NextJS 15 application. Consider using Server Actions or Route Handlers for operations that need to modify cookies.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: