Next.js Discord

Discord Forum

Stale data issue

Answered
American Pipit posted this in #help-forum
Open in Discord
Avatar
American PipitOP
I have a path /employer that has a <Link /> which sends you to /employer/create. In employer/create I have a form that calls a server action:
export async function createEmployer(formData: FormData) {
    'use server';

    const {
        data: { user },
    } = await supabase.auth.getUser();

    const userId = user?.id;
    const name = formData.get('name');
    const description = formData.get('description');

    if (!name || !description || !userId)
        throw new Error('Missing required fields');

    const { error } = await supabase.from('employers').insert({
        name: name?.toString(),
        description: description?.toString(),
        user_id: userId?.toString(),
    });

    if (error) throw new Error(error.message);

    revalidateTag('employers');
    revalidatePath('/employer');

    redirect('/employer');
}

As I wrote - I am revalidating both tag and path, and yet, when using redirect('/employer'), it still shows the old (cached) data. What's weird to me here is that when I manually enter the URL (/employer/create) and then hit create, it shows the new data, I suppose because there is nothing cached yet. I am using unstable_cache inside of my component:
    const employers = await unstable_cache(
        async () => {
            const { data, error } = await supabase
                .from('employers')
                .select()
                .eq('user_id', user?.id);
            if (error) throw error;
            return data;
        },
        ['employers'],
        {
            tags: ['employers'],
            revalidate: 20,
        }
    )();
Answered by Eric Burel
First did you update Next?
View full answer

5 Replies

Avatar
American PipitOP
Also, when recreating this problem in another project, it doesn't seem to have any issues revalidating, even without using unstable_cache(), just with the path.
Avatar
First did you update Next?
Answer
Avatar
it's experimental and there had been severe bugs for a while (and may be in the future)
Avatar
American PipitOP
I updated it and it works.
Thanks, I've been having this issue for a while