`revalidatePath` doesn't work in route handlers

Hey, I am seeking help with on-demand revalidation. Usually I call it in server actions, and there are no issues with that.

This time I use route.ts (I am using useChat from AI SDK, need an endpoint), and I want to check if user has any remaining credits left, if no I want to revalidate specific path, to stop displaying the chat. It should not be visible on the page DURING NEXT VISIT/AFTER REFRESH, not during current session.

That’s crucial part of route handler:

const remainingBalance = ...;
const result = streamText({
      model: ...,

      messages,
      system: ...,
      maxOutputTokens,
      onFinish: async ({ totalUsage }) => {
        logger.info('On-page AI Assistant: Response finished', {
          userId: user.id,
          ...totalUsage,
        });

        const totalTokens = totalUsage.totalTokens;
        await AIService.trackAITokensUsage(user.id, {
          totalTokens,
        });

        if (remainingBalance - totalTokens <= 0) {
          logger.warn('On-page AI Assistant: Page owner has no remaining balance after request', {
            userId: user.id,
            slug,
            remainingBalance,
            totalTokens
          });

          logger.info(
            `[REVALIDATION]: HITTING REVALIDATION FOR ${url.composePathname(Path.Page, slug)}`,
          );
     
          // no results from this call, I would expect revalidation of `/page/${slug}`
          revalidatePath(url.composePathname(Path.Page, slug));
        }
      },
    });

    return result.toUIMessageStreamResponse();

Some facts:

  • the path is 100% correct, I use the same code to revalidate pages in other places (however in server actions).
  • Next.js 15.4.2
  • AI 5.0.0-beta.20
  • Vercel dashboard clearly shows all logs are triggered
  • Desired page (that should be revalidated) uses ISR, can be revalidated by different calls (SA)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.