0
0
NextJSframework~20 mins

Revalidation after mutation in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Revalidation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens after calling a server action that mutates data with revalidation?
Consider a Next.js server action that updates a database and triggers revalidation of a page. What will the user see immediately after the mutation completes?
NextJS
export async function updateData() {
  // mutation logic
  await revalidatePath('/dashboard');
}

export default function Dashboard() {
  const [data, setData] = React.useState(null);
  React.useEffect(() => {
    fetch('/api/data').then(res => res.json()).then(setData);
  }, []);
  return <div>{data?.message}</div>;
}
AThe page reloads fully in the browser to show updated data after mutation.
BThe page shows stale data until the user manually refreshes the browser.
CThe page immediately shows updated data without a full reload because revalidation updates the cache.
DThe page shows an error because revalidation cannot be triggered from server actions.
Attempts:
2 left
💡 Hint
Think about how Next.js cache and revalidation work with server actions.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly triggers revalidation after a mutation in Next.js?
You want to update user info and then revalidate the profile page. Which code correctly calls revalidation after mutation?
A
await updateUser();
await revalidateTag('profile');
B
await updateUser();
revalidatePath('/profile');
C
updateUser();
await revalidatePath('/profile');
D
await updateUser();
await revalidatePath('/profile');
Attempts:
2 left
💡 Hint
Remember to await both mutation and revalidation calls.
🔧 Debug
advanced
2:00remaining
Why does revalidation not update the page after mutation?
You call revalidatePath('/home') after a mutation, but the page still shows old data. What is the most likely cause?
NextJS
export async function updateItem() {
  await mutateDatabase();
  // missing revalidation call
}

export default function Home() {
  const data = fetchData();
  return <div>{data.title}</div>;
}
ANext.js does not support revalidation after mutations.
BThe fetchData function caches data and ignores revalidation.
CYou forgot to call revalidatePath('/home') after mutation.
DThe mutation function mutateDatabase is not awaited.
Attempts:
2 left
💡 Hint
Check if revalidation is triggered after mutation.
🧠 Conceptual
advanced
2:00remaining
What is the effect of revalidateTag compared to revalidatePath in Next.js?
You have a page that uses a cache tag 'profile'. What happens if you call revalidateTag('profile') after a mutation instead of revalidatePath('/profile')?
ArevalidateTag invalidates all cached data with that tag, updating multiple pages if tagged.
BrevalidateTag causes a full browser reload of the page using that tag.
CrevalidateTag only updates the single page at '/profile' and no others.
DrevalidateTag is deprecated and has no effect in Next.js 14.
Attempts:
2 left
💡 Hint
Think about cache tagging and how it groups data.
state_output
expert
2:00remaining
What is the state of a client component after a server action triggers revalidation?
A client component fetches data from a server component. After a server action mutates data and calls revalidatePath, what happens to the client component's state?
NextJS
export default function ClientComponent() {
  const [message, setMessage] = React.useState('Loading...');
  React.useEffect(() => {
    fetch('/api/message').then(res => res.json()).then(data => setMessage(data.text));
  }, []);
  return <p>{message}</p>;
}

// Server action calls revalidatePath('/client') after mutation
AThe client component state remains unchanged until the user refreshes the page.
BThe client component fetches fresh data automatically and updates its state.
CThe client component throws an error because revalidation affects server components only.
DThe client component resets its state to the initial 'Loading...' message.
Attempts:
2 left
💡 Hint
Consider how client components fetch data and how revalidation affects server cache.