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…

10 questions · concept 3 · syntax 2 · practical 3 · logic 2

Concept1. Which statement is the most accurate definition of revalidating data?
Syntax2. Which code-level choice is consistent with revalidating data?
Practical3. A reviewer sees this situation. Which decision best applies revalidating data?
Logic4. What reasoning correctly explains why this approach works for revalidating data?
Concept5. Which boundary does the correct use of revalidating data preserve?
Practical6. What is the safest next step when implementing revalidating data?
Syntax7. Which implementation direction matches the rule for revalidating data?
Logic8. Which consequence follows from applying revalidating data correctly?
Concept9. Which claim about revalidating data is true in this monorepo?
Practical10. Which team practice best demonstrates revalidating data?