You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[`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
64
65
65
66
### 2. Suspense for dynamic data
66
67
@@ -71,21 +72,20 @@ Dynamic data like [`fetch`](/docs/app/api-reference/functions/fetch) calls or da
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.
79
79
80
80
```tsx
81
-
'use cache'
82
81
exportasyncfunction getProducts() {
82
+
'use cache'
83
83
const data =awaitdb.query('SELECT * FROM products')
84
84
returndata
85
85
}
86
86
```
87
87
88
-
## Using Suspense Boundaries
88
+
## Using Suspense boundaries
89
89
90
90
React [Suspense](https://react.dev/reference/react/Suspense) boundaries let you define what fallback UI to use when it wraps dynamic or runtime data.
91
91
@@ -137,17 +137,27 @@ async function DynamicContent() {
137
137
138
138
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.
139
139
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.
141
141
142
142
### Missing Suspense boundaries
143
143
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:
145
145
146
-
> Uncached data was accessed outside of `<Suspense>`
146
+
> **Uncached data was accessed outside of `<Suspense>`**
147
147
>
148
148
> This delays the entire page from rendering, resulting in a slow user
149
149
> experience. Next.js uses this error to ensure your app loads instantly
150
150
> 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>`.
151
161
152
162
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.
153
163
@@ -181,7 +191,7 @@ To reduce network overhead, the full response, including static HTML and streame
181
191
182
192
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.
183
193
184
-
### Basic Usage
194
+
### Basic usage
185
195
186
196
Add `use cache` to cache a page, component, or async function, and define a lifetime with [`cacheLife`](/docs/app/api-reference/functions/cacheLife):
187
197
@@ -251,7 +261,7 @@ export async function CachedWrapper({ children }) {
251
261
252
262
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.
253
263
254
-
### Tagging and Revalidating
264
+
### Tagging and revalidating
255
265
256
266
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.
257
267
@@ -321,7 +331,7 @@ const nextConfig = {
321
331
module.exports= nextConfig
322
332
```
323
333
324
-
### Effect on Route Segment Config
334
+
### Effect on route segment config
325
335
326
336
When Cache Components is enabled, several route segment config options are no longer needed or supported. Here's what changes and how to migrate:
327
337
@@ -412,7 +422,7 @@ export default async function Page() {
412
422
}
413
423
```
414
424
415
-
## Before vs. After Cache Components
425
+
## Before vs. after Cache Components
416
426
417
427
Understanding how Cache Components changes your mental model:
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/04-functions/use-pathname.mdx
+13-4Lines changed: 13 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,8 @@ description: API Reference for the usePathname hook.
5
5
6
6
`usePathname` is a **Client Component** hook that lets you read the current URL's **pathname**.
7
7
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
@@ -34,10 +36,17 @@ For example, a Client Component with `usePathname` will be rendered into HTML on
34
36
> **Good to know**:
35
37
>
36
38
> - 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`.
0 commit comments