Next.js · Senior · Question 55 of 75

With Cache Components, how do you reset a subtree on <Link> navigation but still restore it on browser Back?

const { bfcacheId } = useRouter()
return <Fragment key={bfcacheId}>{children}</Fragment>

Explanation

Routes are hidden with <Activity> rather than unmounted. bfcacheId changes on push/replace but not on back/forward, which mirrors browser bfcache semantics.

Further reading

Problem with a question

Next.js · Senior · question 55

Test yourself

Take the Next.js · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz