Next.js Discord

Discord Forum

Action error: During prerendering, `cookies()` rejects when the prerender is complete

Unanswered
Pumi posted this in #help-forum
Open in Discord
PumiOP
Hello!

Getting the following error/warning.

Action error: During prerendering, `cookies()` rejects when the prerender is complete. Typically these errors are handled by React but if you move `cookies()` to a different context by using `setTimeout`, `after`, or similar functions you may observe this error and you should handle it in that context.


Here is the code for the route where the error occurs:

export default async function ExercisesPage() {
    return (
        <Suspense fallback={<div>Loading...</div>}>
            <ExercisesPageComponent />
        </Suspense>
    );
}

async function ExercisesPageComponent() {
    const exercises = await getExercisesAction();

    return (
        <div className="flex flex-col">
            <H2>Exercises</H2>
            <PMuted>Manage your execises here.</PMuted>

            <div className="flex flex-row mt-4">
                <Button asChild>
                    <Link href="/exercises/new">New exercise</Link>
                </Button>
            </div>

            <div className="mt-6">
                {exercises?.data?.length ? <ExercisesGrid exercises={exercises.data} /> : <P>No exercises found.</P>}
            </div>
        </div>
    );
}


The action:
export const getExercisesAction = actionClient.action(async () => {
    const { user } = await getCurrentSession();

    if (!user) {
        throw new Error('User not found');
    }

    const exercises = await db.query.exercisesTable.findMany({
        where: (table, { eq }) => eq(table.userId, user.id),
    });

    return exercises;
});


Get session function:
export const getCurrentSession = cache(async (): Promise<SessionValidationResult> => {
    const cookieStore = await cookies();
    const token = cookieStore.get('session')?.value ?? null;

    if (token === null) {
        return { session: null, user: null };
    }

    const result = await validateSessionToken(token);

    return result;
});

2 Replies

PumiOP
Anyone have some ideas?
PumiOP
Bump