Pass data from middleware to pages
Answered
Netherland Dwarf posted this in #help-forum
Netherland DwarfOP
Just reiterating the question from another channel, context: https://nextjs-forum.com/post/1247211272508932217#message-1297701151956799538
48 Replies
Netherland DwarfOP
@Arinji
Yeah, getting the same issue with this https://www.npmjs.com/package/nextjs-better-unstable-cache
Why can't you access cookies outside btw?
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?
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;
}
...why use server
Also where are you accessing cookies lol
Netherland DwarfOP
wdym?
Use server is meant for mutations and stuff
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
If your calling it on the server already, you don't need to make it a server action xD
Netherland DwarfOP
ohh
good to know
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
Netherland DwarfOP
ah okay
Could using unstable cache break my code later on?
Say if they released a stable version
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
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
....no so
Get the cookie value at the top
Store it in a varable
And pass that to all the child functions
Netherland DwarfOP
Alright cool, got it
I appreciate it
Np, mark a solution if it helped :)