Next.js Discord

Discord Forum

revalidateTag() in route.ts before redirect() doesn't do anything

Answered
American posted this in #help-forum
Open in Discord
AmericanOP
Hello

I'm on next@14.2.13

I'm a route handler, I'm doing the following:

revalidateTag(`myGroups=${session.user.userId}`); // Does not bust cache
await sleep(1000);
redirect("/");


but the unstable_cache cache is never invalidated. I can invalidate in server actions.

How can I both revalidate and redirect in the same route?
Answered by American
I couldn't make it work, I had to switch to
return NextResponse.redirect(new URL(path, request.url), 307)
View full answer

39 Replies

yeah, you should be able to revalidate and redirect in the same route
also I don't think you need await sleep(1000) if it's manual waiting
AmericanOP
I couldn't make it work, I had to switch to
return NextResponse.redirect(new URL(path, request.url), 307)
Answer
revalidateTag(`myGroups=${session.user.userId}`); // Does not bust cache
await sleep(1000);
redirect("/");

When do you hit this api rotue btw?
AmericanOP
It's a invite link, so the user gets it via email or similar
so the invite link is heading to the api route?
but why do we use revalidateTag? it's about purging cache - just curious about how it's related to invitation
AmericanOP
Yes, exactly

The file is located here: src/app/invite/[hash]/route.ts
The user gets an email with
http://localhost/invite/somesecrethash
When the user visits this route I check the hash etc, then I add the user to the group.

Now I need to clear the cache for "myGroups" so the user can see the group in their dashboard
okay so / must be your dashboard page where you list all of the members, right?
show me how you fetch your members
AmericanOP
I'm using a package called nextjs-better-unstable-cache which combines reacts cache with unstable_cache
export const getMyGroups_CACHED = memoize(getMyGroups_UNSAFE, {
  persist: true,
  duration: 60,
  revalidateTags: ({ userId }) => ["groups", `myGroups=${userId}`],
});

export async function getMyGroups_UNSAFE(user: User) {
  const userWithGroups = await db.query.usersToGroups.findMany({
    where: eq(usersToGroups.userId, user.userId),
  });

  const groups = userWithGroups
    .map((g) => g.group)
  return groups;
}
And it works fine when I use NextResponse.redirect
nextjs-better-unstable-cache
First time to hear about this library - unstable_cache is not enough?
AmericanOP
It's using https://react.dev/reference/react/cache to memoize the call too
hmm wait, I feel like revalidatePath() should be enough, no?
when a new user joins, we purge cache.
AmericanOP
I don't want to purge for every group, I'm actually doing two revalidations for some more context

revalidateTag(`myGroups=${session.user.userId}`);
revalidateTag(`group=${groupId}`);
what are the groups? I don't have any context over there
invite hash has group info?
AmericanOP
A group has several members. The invite hash is stored in my database with a groupId
But I think the issue is with how redirect work with throwing an error
redirect internally throws an error but as long as you don't use redirect inside try catch, it should be fine
return NextResponse.redirect(new URL(path, request.url), 307)

This is the same as hard-navigation, that's why you don't see any stale data I guess
AmericanOP
Yeah, and I think it's fine in this case, since the user is hard-navigating to this URL in the first case
yeah, true!
btw can you show me your dashboard page? is your app authenticated? how do you get gorup_id from a user?
AmericanOP
I'm using Lucia Auth, which has a function called getUserSession()
Then I have a table called "usersToGroups" which maps the relations between users and groups
oh, makes sense.
hmm but when a user gets invitation link and they visit your website
and then I assume they need to login?
AmericanOP
Yeah, when visiting the invite link I check if the user is logged in, if not then I set a cookie called redirect-after-login, and redirect the user to the login page. After login they are redirect to the invite link
oh makes sense
@American Yeah, and I think it's fine in this case, since the user is hard-navigating to this URL in the first case
Then I think, NextResponse.redirect is not that a good solution
you should have noticable delay, no?
AmericanOP
It's almost instant
or instant enough 😇
haha then np
AmericanOP
Thanks for taking the time James! I enjoyed chatting with you