Commit fa56f2c
fix: revalidateTag with profile should not trigger client cache invalidation (#88069)
## Summary
Fixes a bug where calling `revalidateTag(tag, profile)` incorrectly
triggers client-side cache invalidation, causing read-your-own-writes
behavior that violates stale-while-revalidate semantics.
### The Problem
When `revalidateTag('tag', 'max')` is called in a server action:
1. The tag is correctly marked for stale-while-revalidate
2. BUT the `x-action-revalidated` header is incorrectly set to `1`
3. This triggers client-side cache invalidation via
`revalidateEntireCache()`
4. The client navigates and may display stale data from background
revalidation
This caused the confusing behavior where:
- Click 1: Nothing happens (correct)
- Click 2: Nothing happens (correct)
- Click 3: Data changes to a stale value from click 1 (incorrect!)
### The Fix
In `addRevalidationHeader`, change the `isTagRevalidated` calculation to
only count tags **without** a profile (from `updateTag`). Tags with a
profile (from `revalidateTag`) should follow stale-while-revalidate
semantics and not trigger immediate client-side cache invalidation.
```typescript
// Before:
const isTagRevalidated = workStore.pendingRevalidatedTags?.length ? 1 : 0
// After:
const isTagRevalidated = workStore.pendingRevalidatedTags?.some(
(item) => item.profile === undefined
) ? 1 : 0
```
### Expected Behavior After Fix
| API | Profile | Header Set | Client Behavior |
|-----|---------|------------|-----------------|
| `updateTag(tag)` | `undefined` | ✅ Yes | Immediate refresh
(read-your-own-writes) |
| `revalidateTag(tag, 'max')` | `'max'` | ❌ No | Keep stale data (SWR) |
| `revalidateTag(tag, { expire: 0 })` | `{ expire: 0 }` | ✅ Yes* |
Immediate refresh |
*For immediate expiration via `pathWasRevalidated` which is already
handled correctly.
## Test Plan
- [x] Added new test page at
`test/e2e/app-dir/use-cache/app/(partially-static)/revalidate-tag-no-refresh/page.tsx`
- [x] Added test case that verifies 3 clicks of `revalidateTag` with
profile don't change the displayed value
- [x] Verified existing `updateTag` test (`should update after
revalidateTag correctly`) still passes
- [x] Verified both tests pass with `pnpm test-start`
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.5 <[email protected]>1 parent 8aa9feb commit fa56f2c
File tree
3 files changed
+78
-1
lines changed- packages/next/src/server/app-render
- test/e2e/app-dir/use-cache
- app/(partially-static)/revalidate-tag-no-refresh
3 files changed
+78
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
164 | 171 | | |
165 | 172 | | |
166 | 173 | | |
| |||
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
495 | 495 | | |
496 | 496 | | |
497 | 497 | | |
| 498 | + | |
498 | 499 | | |
499 | 500 | | |
500 | 501 | | |
| |||
679 | 680 | | |
680 | 681 | | |
681 | 682 | | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
682 | 718 | | |
683 | 719 | | |
684 | 720 | | |
| |||
0 commit comments