Next.js Discord

Discord Forum

Unable to cache authenticated requests

Unanswered
Ichneumonid wasp posted this in #help-forum
Open in Discord
Ichneumonid waspOP
Hello, I am using Nextjs 16 with cached components and Supabase for my auth. In my current setup I enforce authentication at 4 layers. 1) Proxy file is the first place where I redirect, 2) Protected routes enforce auth themselves, 3) Server Actions are all authed and 4) my DAL functions all require auth.

I am trying to cache my DAL functions because I feel re-fetching profile data over and over every time the user navigates to or refreshers the profile page is not the proper way of handling it. However for some reason nothing gets cached. I am using cache:private to make sure each request is cached per session.

Is this the correct way to go about this? How would I cache user data so I dont have to refetch everything on navigation? Here is an example of one my DAL methods:

const getSubscriptionByUserIdCached = async (user_id: string, fieldString: string, authenticated: boolean) => {
    'use cache: private';
    cacheLife('minutes');
    cacheTag(`user-subscription-${user_id}`);

    if (!authenticated) throw new UnauthorizedError("User not authorized");
    const supabase = await createClient();

    const { data, error } = await supabase
        .from("subscription")
        .select(fieldString as "*")
        .eq("user_id", user_id)
        .maybeSingle();

    return data;
};

export class SubscriptionDAL {
    private constructor(private readonly authenticated: boolean = false) { }

    static async create(): Promise<SubscriptionDAL> {
        await requireAuth();
        return new SubscriptionDAL(true);
    }

    getSubscriptionByUserId = async (user_id: string, fields?: string[]) => {
        const fieldString = fields?.length ? fields.join(",") : "*";
        return getSubscriptionByUserIdCached(user_id, fieldString, this.authenticated);
    };

EDIT: supabase's createClient() uses the cookie API to ready the user and create an Authenticated client. That means that on any place I check auth the route becomes dynamic (??)

3 Replies

@Ichneumonid wasp your are using the mins profile in cache life so

" (alias) cacheLife(profile: "minutes"): void (+15 overloads)
import cacheLife
Cache this "use cache" for a timespan defined by the "minutes" profile.

stale: 300 seconds (5 minutes)
revalidate: 60 seconds (1 minute)
expire: 3600 seconds (1 hour)
This cache may be stale on clients for 5 minutes before checking with the server. If the server receives a new request after 1 minute, start revalidating new values in the background. If this entry has no traffic for 1 hour it will expire. The next request will recompute it."
if the page is refreshed after 60 seconds it will use new data
about the route being dynamic yes at build time 'use cache: private' doesnt not create a cached version of your page but caches it in the browser with the profile you have selected. so yes the route is dynamic at build time but navagtions will be cached, I suggest a profile that has a higher revalidate