Next.js Discord

Discord Forum

Pass data from middleware to pages

Answered
Netherland Dwarf posted this in #help-forum
Open in Discord
Avatar
Netherland DwarfOP
Just reiterating the question from another channel, context: https://nextjs-forum.com/post/1247211272508932217#message-1297701151956799538

48 Replies

Avatar
Netherland DwarfOP
@Arinji
Yeah, getting the same issue with this https://www.npmjs.com/package/nextjs-better-unstable-cache
Avatar
Why can't you access cookies outside btw?
Avatar
Netherland DwarfOP
My supabase client
async function getDashboardUser() {
  const account = await getCurrentUser(true);
  const user = await getUserById(account.id);

  return user;
}

export const getCachedUser = memoize(() => getDashboardUser(), {
  revalidateTags: () => ["user"],
});
This is what I'm doing, would there be a better way?
Avatar
Yea, show the other 2 functions
get current user
Get user id
Avatar
Netherland DwarfOP
"use server";

import { redirect } from "next/navigation";
import { createSupabaseServerClient } from "../server";
import { User } from "@supabase/supabase-js";

export async function getCurrentUser(redirectUser?: true): Promise<User>;
export async function getCurrentUser(redirectUser?: boolean) {
  const supabase = await createSupabaseServerClient();
  const { data } = await supabase.auth.getUser();

  if (redirectUser && !data) return redirect("/login");
  return data.user;
}
export async function getUserById(id: string) {
  const supabase = await db();

  const { data: user, error } = await supabase.from("users").select("id, email, name, verified, image").eq("id", id).limit(1).single();
  return user;
}
Avatar
...why use server :fine:
Also where are you accessing cookies lol
Avatar
Netherland DwarfOP
wdym?
Avatar
Use server is meant for mutations and stuff
Avatar
Netherland DwarfOP
export async function createSupabaseServerClient() {
  const cookieStore = cookies();

  return createServerClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_KEY!, {
    cookies: {
      getAll() {
        return cookieStore.getAll();
      },
      setAll(cookiesToSet) {
        try {
          cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options));
        } catch {
        }
      },
    },
  });
}
here
Avatar
If your calling it on the server already, you don't need to make it a server action xD
Avatar
Netherland DwarfOP
ohh
good to know
Avatar
Ok so
See the whole cookie stuff?
async function getDashboardUser() {
//Cookie stuff here
unstable cache()
  const account = await getCurrentUser(true);
  const user = await getUserById(account.id);

  return user;
}
//Don't do this
export const getCachedUser = memoize(() => getDashboardUser(), {
  revalidateTags: () => ["user"],
});
So get the cookie stuff outside of the cached part
And make unstable cache make new cache records for each cookie value
Avatar
Netherland DwarfOP
ah okay
Could using unstable cache break my code later on?
Say if they released a stable version
Avatar
Eh, we call it the most stable part of caching in nextjs lmao
Cause it hasn't changed since it got released
They will release codemodd tbf, it's too much used to just forget
Avatar
Netherland DwarfOP
Makes sense
So something like this:
export async function getDashboardUser() {
  const account = await getCurrentUser(true);
  const user = await getUserById(account.id);

  return await memoize(
    () => {
      return user;
    },
    {
      revalidateTags: () => ["user"],
    }
  )();
}
Appreciate it, noticed your 17 as well props on you for knowing this stuff. I'm barely getting a grip on next 😅
wait
doesn't that ruin the point of the cache?
I moved the user into the cache function, but wouldn't it still call the getCurrentUser whenever I call getDashboardUser
Avatar
....no so
Get the cookie value at the top
Store it in a varable
And pass that to all the child functions
Answer
Avatar
See here
How I get the cookie in the top level
And pass it down to my cached functions
Avatar
Netherland DwarfOP
Alright cool, got it
I appreciate it
Avatar
Np, mark a solution if it helped :)