Fullstack CourseLearn by building
Back to week 3

Topic

Revalidate stale data

Definition

Revalidation marks cached data or rendered output stale so a later request obtains a fresh representation.

In simpler words

After a change, tell the cache that its old answer is no longer safe to reuse.

The correct revalidation mechanism depends on which cache contains the stale representation.

After this you can

  • Invalidate browser queries after a Nest write
  • Explain path versus tag revalidation
  • Avoid using revalidation as authorization

Refresh the layer that is stale

A client ticket list cached by TanStack Query should be invalidated through Query after a successful mutation. That marks the corresponding browser resource stale and lets active observers refetch it.

revalidatePath and revalidateTag are Next server-cache mechanisms. They matter only when the affected server-rendered route or tagged fetch is actually using that caching strategy.

Invalidate a ticket list

onSuccess: (_ticket, variables) => {
  queryClient.invalidateQueries({ queryKey: ['tickets'] });
  queryClient.invalidateQueries({ queryKey: ['ticket', variables.id] });
}

Invalidate the list and detail representation that a completed write made stale.

Mistake: calling a Next API for a Query cache

// Wrong — does not refresh browser Query data
revalidatePath('/tickets');

// Right for a client Query list
queryClient.invalidateQueries({ queryKey: ['tickets'] });

Revalidation APIs are scoped to their own cache layer.

Use paths and tags precisely

Definition

Path revalidation targets rendered output for a route; tag revalidation targets server fetches that share a declared tag.

In simpler words

Tags are useful when several routes depend on the same server-side resource.

Do not add tags before you know which server-rendered resources must refresh together.

A revalidated representation still passes through the same backend authorization rules on its next request.

Live playground

Revalidating playground

Choose the cache layer that must refresh after a ticket edit.

Query cache: stale

Next revalidatePath/tag is for Next server caches — not Nest auth.

Keep in mind

  • Invalidate Query after interactive Nest mutations.
  • Use revalidatePath or revalidateTag only for Next server caches you actually use.
  • Match the invalidation scope to the changed resource.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥80% (enforced by the API) to save progress.

Checking your session…

15 questions · concept 5 · syntax 3 · practical 4 · logic 3

1. What is revalidation?
Concept
2. Time-based revalidate?
Syntax
3. On-demand revalidation?
Practical
4. Tagging fetch caches helps how?
Logic
5. After creating a post in a blog?
Concept
6. Client Query vs Next cache?
Practical
7. Too aggressive revalidate?
Syntax
8. Webhook-driven revalidate?
Logic
9. Stale-while-revalidate mindset?
Concept
10. Which is correct?
Practical
11. What does this do after a mutation?
Conceptintermediate
import { revalidatePath } from 'next/cache';
revalidatePath('/blog');
12. What does tagging enable here?
Syntaxadvanced
fetch(url, { next: { tags: ['tickets'] } });
// later:
revalidateTag('tickets');
13. What does this segment export configure?
Practicalintermediate
export const revalidate = 3600;
14. What pattern is this?
Logicadvanced
// CMS publishes -> POST /api/revalidate
export async function POST(req) { revalidateTag('posts'); }
15. Both caches hold the tickets list. After a mutation, what must you do?
Conceptintermediate
// both TanStack Query and the Next cache hold the tickets list

Checking your session…