Skip to content

Commit 4b45979

Browse files
authored
Docs/use cache feedback 2 (#85222)
- [x] Update Error
1 parent 0b95122 commit 4b45979

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

docs/01-app/01-getting-started/06-cache-components.mdx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ When a user visits a route:
4848

4949
> **🎥 Watch:** Why PPR and how it works → [YouTube (10 minutes)](https://www.youtube.com/watch?v=MTcPrTIBkpA).
5050
51-
## How it Works
51+
## How it works
5252

5353
Cache Components gives you three key tools to control rendering:
5454

@@ -61,6 +61,7 @@ Some data is only available at runtime when an actual user makes a request. APIs
6161
- [`cookies`](/docs/app/api-reference/functions/cookies)
6262
- [`headers`](/docs/app/api-reference/functions/headers)
6363
- [`searchParams` prop](/docs/app/api-reference/file-conventions/page#searchparams-optional)
64+
- [`params` prop](/docs/app/api-reference/file-conventions/page#params-optional) - This is runtime data unless you provide at least one example value through [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params). When provided, those specific param values are treated as static for prerendered paths, while other values remain runtime
6465

6566
### 2. Suspense for dynamic data
6667

@@ -71,21 +72,20 @@ Dynamic data like [`fetch`](/docs/app/api-reference/functions/fetch) calls or da
7172
- [`fetch`](/docs/app/api-reference/functions/fetch) requests
7273
- Database queries
7374
- [`connection`](/docs/app/api-reference/functions/connection)
74-
- [`unstable_noStore`](/docs/app/api-reference/functions/unstable_noStore)
7575

7676
### 3. Cached data with `use cache`
7777

7878
Add `use cache` to any Server Component to make it cached and include it in the pre-rendered shell. You cannot use runtime APIs from inside a cached component. You can also mark utility functions as `use cache` and call them from Server Components.
7979

8080
```tsx
81-
'use cache'
8281
export async function getProducts() {
82+
'use cache'
8383
const data = await db.query('SELECT * FROM products')
8484
return data
8585
}
8686
```
8787

88-
## Using Suspense Boundaries
88+
## Using Suspense boundaries
8989

9090
React [Suspense](https://react.dev/reference/react/Suspense) boundaries let you define what fallback UI to use when it wraps dynamic or runtime data.
9191

@@ -137,17 +137,27 @@ async function DynamicContent() {
137137

138138
At build time, Next.js pre-renders the static content and the `fallback` UI, while the dynamic content is postponed until a user requests the route.
139139

140-
Wrapping a component in `Suspense` doesn't make it dynamic; your API usage does. `Suspense` acts as a boundary that encapsulates dynamic content and enables streaming.
140+
> **Good to know**: Wrapping a component in `Suspense` doesn't make it dynamic; your API usage does. `Suspense` acts as a boundary that encapsulates dynamic content and enables streaming.
141141
142142
### Missing Suspense boundaries
143143

144-
Cache Components enforces that dynamic code must be wrapped in a `Suspense` boundary. If you forget, you'll see this error:
144+
Cache Components enforces that dynamic code must be wrapped in a `Suspense` boundary. If you forget, you'll see the [Uncached data was accessed outside of `<Suspense>`](https://nextjs.org/docs/messages/blocking-route) error:
145145

146-
> Uncached data was accessed outside of `<Suspense>`
146+
> **Uncached data was accessed outside of `<Suspense>`**
147147
>
148148
> This delays the entire page from rendering, resulting in a slow user
149149
> experience. Next.js uses this error to ensure your app loads instantly
150150
> on every navigation.
151+
>
152+
> To fix this, you can either:
153+
>
154+
> **Wrap the component in a `<Suspense>`** boundary. This allows Next.js to stream its contents to the user as soon as it's ready, without blocking the rest of the app.
155+
>
156+
> or
157+
>
158+
> **Move the asynchronous await into a Cache Component("use cache")**. This allows Next.js to statically prerender the component as part of the HTML document, so it's instantly visible to the user.
159+
>
160+
> Note that request-specific information, such as params, cookies, and headers, is not available during static prerendering, so it must be wrapped in `<Suspense>`.
151161
152162
This error helps prevent a situation where, instead of getting a static shell instantly, users would hit a blocking runtime render with nothing to show. To fix it, add a `Suspense` boundary or use `use cache` to cache the work instead.
153163

@@ -181,7 +191,7 @@ To reduce network overhead, the full response, including static HTML and streame
181191

182192
While `Suspense` boundaries manage dynamic content, the [`use cache`](/docs/app/api-reference/directives/use-cache) directive is available for caching data or computations that don't change often.
183193

184-
### Basic Usage
194+
### Basic usage
185195

186196
Add `use cache` to cache a page, component, or async function, and define a lifetime with [`cacheLife`](/docs/app/api-reference/functions/cacheLife):
187197

@@ -251,7 +261,7 @@ export async function CachedWrapper({ children }) {
251261

252262
You must not pass dynamic or runtime data into `use cache` functions unless you avoid introspecting them. Passing values from `cookies()`, `headers()`, or other runtime APIs as arguments will cause errors, as the cache key cannot be determined at pre-render time.
253263

254-
### Tagging and Revalidating
264+
### Tagging and revalidating
255265

256266
Tag cached data with [`cacheTag`](/docs/app/api-reference/functions/cacheTag) and revalidate it after mutations using [`updateTag`](/docs/app/api-reference/functions/updateTag) in Server Actions for immediate updates, or [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) delay in updates are acceptable.
257267

@@ -321,7 +331,7 @@ const nextConfig = {
321331
module.exports = nextConfig
322332
```
323333

324-
### Effect on Route Segment Config
334+
### Effect on route segment config
325335

326336
When Cache Components is enabled, several route segment config options are no longer needed or supported. Here's what changes and how to migrate:
327337

@@ -412,7 +422,7 @@ export default async function Page() {
412422
}
413423
```
414424

415-
## Before vs. After Cache Components
425+
## Before vs. after Cache Components
416426

417427
Understanding how Cache Components changes your mental model:
418428

docs/01-app/03-api-reference/04-functions/use-pathname.mdx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ description: API Reference for the usePathname hook.
55

66
`usePathname` is a **Client Component** hook that lets you read the current URL's **pathname**.
77

8+
> **Good to know**: When [`cacheComponents`](/docs/app/api-reference/config/next-config-js/cacheComponents) is enabled `usePathname` may require a `Suspense` boundary around it if your route has a dynamic param. If you use `generateStaticParams` the `Suspense` boundary is optional
9+
810
```tsx filename="app/example-client-component.tsx" switcher
911
'use client'
1012

@@ -34,10 +36,17 @@ For example, a Client Component with `usePathname` will be rendered into HTML on
3436
> **Good to know**:
3537
>
3638
> - Reading the current URL from a [Server Component](/docs/app/getting-started/server-and-client-components) is not supported. This design is intentional to support layout state being preserved across page navigations.
37-
> - Compatibility mode:
38-
> - `usePathname` can return `null` when a [fallback route](/docs/pages/api-reference/functions/get-static-paths#fallback-true) is being rendered or when a `pages` directory page has been [automatically statically optimized](/docs/pages/building-your-application/rendering/automatic-static-optimization) by Next.js and the router is not ready.
39-
> - When using `usePathname` with rewrites in [`next.config`](/docs/app/api-reference/config/next-config-js/rewrites) or [`Proxy`](/docs/app/api-reference/file-conventions/proxy), `useState` and `useEffect` must also be used in order to avoid hydration mismatch errors.
40-
> - Next.js will automatically update your types if it detects both an `app` and `pages` directory in your project.
39+
> - If your page is being statically pre-rendered and your app has [rewrites](/docs/app/api-reference/config/next-config-js/rewrites) in `next.config` or a [Proxy](/docs/app/api-reference/file-conventions/proxy) file, reading the pathname with `usePathname()` can result in hydration mismatch errors—because the initial value comes from the server and may not match the actual browser pathname after routing. See our [example](#avoid-hydration-mismatch-with-rewrites) for a way to mitigate this issue.
40+
41+
<details>
42+
43+
<summary>Compatibility with Pages Router</summary>
44+
45+
If you have components that use `usePathname` and they are imported into routes within the Pages Router, be aware that `usePathname` may return `null` if the router is not yet initialized. This can occur in cases such as [fallback routes](/docs/pages/api-reference/functions/get-static-paths#fallback-true) or during [Automatic Static Optimization](https://nextjs.org/docs/pages/building-your-application/rendering/static#automatic-static-optimization) in the Pages Router.
46+
47+
To enhance compatibility between routing systems, if your project contains both an `app` and a `pages` directory, Next.js will automatically adjust the return type of `usePathname`.
48+
49+
</details>
4150

4251
## Parameters
4352

0 commit comments

Comments
 (0)