Skip to content

Commit a03b8ee

Browse files
docs: link Activity guide from Cache Components migration guide (#92571)
## Summary - Add a "UI state preservation" section to the Cache Components migration guide explaining that pages are now preserved via React's `<Activity>` instead of unmounting on navigation - Add `related` links pointing to the preserving UI state guide and `cacheComponents` config reference - Cross-link the migration guide from the preserving UI state guide for bidirectional discovery The migration guide previously only covered route segment config replacements (`dynamic`, `revalidate`, `fetchCache`, `runtime`) but didn't mention the fundamental navigation behavior change. Users migrating to Cache Components need to know that cleanup logic, analytics events, and state resets may behave differently. --------- Co-authored-by: Joseph Chamochumbi <joseph.chamochumbi@vercel.com>
1 parent 645eef5 commit a03b8ee

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

docs/01-app/01-getting-started/08-caching.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ related:
88
- app/getting-started/revalidating
99
- app/api-reference/directives/use-cache
1010
- app/api-reference/config/next-config-js/cacheComponents
11+
- app/guides/preserving-ui-state
1112
---
1213

1314
{/* AI agent hint: To ensure client-side navigations are instant with Cache Components, export `unstable_instant` from the route. See docs/01-app/02-guides/instant-navigation.mdx */}

docs/01-app/02-guides/migrating-to-cache-components.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
title: Migrating to Cache Components
33
nav_title: Migrating to Cache Components
44
description: Learn how to migrate from route segment configs to Cache Components in Next.js.
5+
related:
6+
title: Next Steps
7+
description: Learn about other behavior changes when Cache Components is enabled.
8+
links:
9+
- app/guides/preserving-ui-state
10+
- app/api-reference/config/next-config-js/cacheComponents
511
---
612

713
When [Cache Components](/docs/app/api-reference/config/next-config-js/cacheComponents) is enabled, route segment configs like `dynamic`, `revalidate`, and `fetchCache` are replaced by [`use cache`](/docs/app/api-reference/directives/use-cache) and [`cacheLife`](/docs/app/api-reference/functions/cacheLife).
@@ -173,3 +179,15 @@ export default async function Page() {
173179
## `runtime = 'edge'`
174180

175181
**Not supported.** Cache Components requires the Node.js runtime. Switch to the Node.js runtime (the default) by removing the `runtime = 'edge'` export. If you need edge behavior for specific routes, use [Proxy](/docs/app/api-reference/file-conventions/proxy) instead.
182+
183+
## UI state preservation
184+
185+
**Component state now persists across navigations.** With Cache Components, Next.js preserves routes using React's [`<Activity>`](https://react.dev/reference/react/Activity) component in [`"hidden"`](https://react.dev/reference/react/Activity#activity) mode instead of unmounting them. Effects clean up and re-run normally, but `useState` values, form inputs, and scroll position are no longer reset when navigating away and back.
186+
187+
If your code relied on unmounting to clear state, you may need to add explicit reset logic:
188+
189+
- **Dropdowns and popovers** — stay open when navigating back. Close them in a `useLayoutEffect` cleanup function.
190+
- **Dialogs with initialization logic** — Effects that depend on dialog state (like focusing an input) won't re-fire if the state was preserved. Derive dialog state from the URL instead.
191+
- **Forms after submission** — input values and `useActionState` results (success/error messages) persist when returning. Reset in the submit handler or user action when possible, otherwise use a cleanup effect.
192+
193+
See [Preserving UI state across navigations](/docs/app/guides/preserving-ui-state) for detailed examples of each pattern.

docs/01-app/02-guides/preserving-ui-state.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
---
2-
title: Preserving UI state across navigations
2+
title: How Next.js preserves UI state with Activity
33
nav_title: Preserving UI state
4-
description: Learn how to control which UI state is preserved and which resets when navigating between pages.
4+
description: Learn how React's Activity component preserves UI state across navigations in Next.js and how to control what resets.
55
related:
66
title: Related
77
description: Learn more about Cache Components and preserving UI state.
88
links:
99
- app/getting-started/caching
10+
- app/guides/migrating-to-cache-components
1011
---
1112

1213
> **Good to know:** This guide assumes [Cache Components](/docs/app/getting-started/caching) is enabled. Enable it by setting [`cacheComponents: true`](/docs/app/api-reference/config/next-config-js/cacheComponents) in your Next config file.
1314
1415
Before Cache Components, preserving page-level state across navigations required workarounds like hoisting state to a [shared layout](/docs/app/getting-started/layouts-and-pages#nesting-layouts) or using an external store. With Cache Components, Next.js preserves state and DOM out of the box.
1516

16-
Instead of unmounting pages on navigation, Next.js hides them using React's [`<Activity>`](https://react.dev/reference/react/Activity) component. The DOM nodes stay in the document (hidden with `display: none`), so both React state and DOM state are preserved: form drafts, scroll positions, expanded `<details>` elements, video playback progress, and more.
17+
Instead of unmounting pages on navigation, Next.js hides them using React's [`<Activity>`](https://react.dev/reference/react/Activity) component. Activity keeps the DOM in the document (hidden with `display: none`), so both React state and DOM state are preserved: form drafts, scroll positions, expanded `<details>` elements, video playback progress, and more.
1718

1819
Next.js preserves up to 3 routes. Beyond that, the oldest route is evicted and will re-render fresh.
1920

0 commit comments

Comments
 (0)