Next.js Discord

Discord Forum

Using generateStaticParams to statically generate routes at build time gives an error

Answered
Black-throated Sparrow posted this in #help-forum
Open in Discord
Black-throated SparrowOP
I'm trying to statically generate my app routes by build time in my dynamic route in the following manner:
 js
const generateStaticParams = async () => {
  // Initialise Supabase client
  const cookieStore =  cookies();
  const supabase: SupabaseClient = createServerComponentClient({
    cookies: () => cookieStore,
  });

  const { data: events } = await supabase.from("events").select("id");

  return events?.map(({ id }) => ({
    id,
  }));
};

const Event = async ({ params: { id } }: { params: { id: string } }) => {
  // Initialise Supabase client
  const cookieStore = cookies();
  const supabase: SupabaseClient = createServerComponentClient({
    cookies: () => cookieStore,
  });

  // Retrieve session data
  const session = await getSession();

  const { data: event } = await supabase.from("events").select().eq("id", id).single();

  return <div>{event.name}</div>;
};

export {generateStaticParams};
export default Event;

However whenever I do so I get the following error:
Error: Invariant: Method expects to have requestAsyncStorage, none available

This is weird because it's the exact same method that I've been using to initialise a supabase client in my entire application (the reason I've been redoing it within each file is because Vercel doesn't like it very much when I put those two lines in a single script and call that function each time).

If I get rid of the generateStaticParams function the page works fine. Of course I could just not use the function, although that sort of defeats the whole purpose of SSR so I'd like to have that extra optimisation if possible. Any ideas why this is the case?

As always, thank you all in advance :)
Answered by alfon
cookies doesn't exist on build time...
View full answer

9 Replies

@Black-throated Sparrow I'm trying to statically generate my app routes by build time in my dynamic route in the following manner: js const generateStaticParams = async () => { // Initialise Supabase client const cookieStore = cookies(); const supabase: SupabaseClient = createServerComponentClient({ cookies: () => cookieStore, }); const { data: events } = await supabase.from("events").select("id"); return events?.map(({ id }) => ({ id, })); }; const Event = async ({ params: { id } }: { params: { id: string } }) => { // Initialise Supabase client const cookieStore = cookies(); const supabase: SupabaseClient = createServerComponentClient({ cookies: () => cookieStore, }); // Retrieve session data const session = await getSession(); const { data: event } = await supabase.from("events").select().eq("id", id).single(); return <div>{event.name}</div>; }; export {generateStaticParams}; export default Event; However whenever I do so I get the following error: Error: Invariant: Method expects to have requestAsyncStorage, none available This is weird because it's the exact same method that I've been using to initialise a supabase client in my entire application (the reason I've been redoing it within each file is because Vercel doesn't like it very much when I put those two lines in a single script and call that function each time). If I get rid of the `generateStaticParams` function the page works fine. Of course I could just not use the function, although that sort of defeats the whole purpose of SSR so I'd like to have that extra optimisation if possible. Any ideas why this is the case? As always, thank you all in advance :)
cookies doesn't exist on build time...
Answer
thats why it throws an error. basically saying cookies doesn't exist (none available)
@alfon cookies doesn't exist on build time...
Black-throated SparrowOP
Just initialise the supabase client without cookies then?
yep
or maybe provide the necessary info that would be in the cookies, straight to supabase
or initialize the supabase singleton so that you would only have to initialize it once and export it to wherever you need it
Black-throated SparrowOP
Such a stupid thing if I even thought about it for more than 2 seconds lmao, I've been doing it too much throughout my application
@alfon yep
Black-throated SparrowOP
Yeah just making the first supabase client instantiation a client component instead of a server component immediately fixed the issue
Thanks man!