next.js: Revalidation of multiple tags at once fails

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #83-Ubuntu SMP Thu Jun 15 19:16:32 UTC 2023
    Binaries:
      Node: 18.17.0
      npm: 9.6.7
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.12
      eslint-config-next: 13.4.9
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Data fetching (gS(S)P, getInitialProps)

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/dazzling-wozniak-l3f9sn?file=%2Fapp%2Fapi%2Frevalidate%2Froute.ts%3A4%2C33

To Reproduce

Explanation:

Doing either revalidateTag or revalidatePath multiple times during a request. I recreated this in the codesandbox here.

To test this, I have an API returning a timestamp to know if the cache has revalidated app/api/tags/fake-data. I then have an API route calling that API twice with different params to have two separate tags, first for tag1, second for tag2. (app/api/tags) I then have an API to revalidate both of those tags under app/api/revalidate.

Steps:

  1. Navigate to /api/tags and it should output two timestamps. These will now be cached under tags tag1 and tag2
  2. In a second tab, open /api/revalidate, this will revalidate both of those tags in the single call.
  3. Go and refresh the first tab and you will see only one of them has changed. If you go to the file app/api/revalidate, you can comment out either one and by themselves they work normally, it’s only 2+ calls to revalidateTag that causes revalidation to not occur.

Describe the Bug

When revalidating specific tags or paths inside of a request like so.

import { revalidateTag } from "next/cache";
import { NextResponse } from "next/server";

type revalidateProps = {
    routes : string[],
}

export async function POST(req) {
    const body = await req.json();
    try {
        const { routes } = body as revalidateProps;
        routes.forEach(function (route) {
            revalidateTag(route);
        });
        return NextResponse.json({ revalidated: true, now: Date.now() }, {status: 200});
    } catch (err) {
        return NextResponse.json({message: `Error revalidating error: ${err}`}, {status: 500});
    }
}

This causes either none of the tags to be revalidated or only part of them. Currently to get around this we are doing an API call per tag.

When revalidating only one tag in each request the revalidation works perfectly, its only when trying to call the revalidateTag function more than one time (for example in a for loop) that this bug occurs.

Expected Behavior

All tags that are revalidated using revalidateTag during a request should be revalidated. This could perhaps even be solved by adding a revalidateTags to avoid individual calls.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

Docker (node image)

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 12
  • Comments: 15 (1 by maintainers)

Most upvoted comments

I’d recommend reopening this issue. On 13.5.2, still not okay.

revalidateTag(body._type);
    if (body.slug) {
      revalidateTag(`${body._type}:${body.slug}`);
    }

Given having both a body._type & body.slug it only revalidates one of them.

Having this issue as well.

In my case, I have a client event handler with two revalidatePath() server actions.

revalidatePath('/shelf/foobar')
revalidatePath('/reviews')

Only the top one gets executed. So weird. I just simply don’t understand how next JS caching works sometimes, no matter how many times I comb through their docs.

@thedevdavid Just tested this in this sandbox by navigating to /api/revalidate to set the tags, going to /api/tags to check the current and in a new tab running /api/revalidate again. The numbers displayed are different in both tags, meaning it is functioning using this example.

Yes, sorry, it doesn’t really work. It just randomly triggers one of the revalidateTag functions.

That’s unfortunate. I am not able to migrate to the app directory since my project is listening to a webhook that is supposed to revalidate multiple pages based on a ‘for of’ loop. Unfortunately, it doesn’t work as expected.

Hope they will fix it soon because I’am stuck at the time

Same for me, I upgraded to 13.4.19 and seems to work now