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
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/04-functions/revalidatePath.mdx
+63-5Lines changed: 63 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,25 +5,83 @@ description: API Reference for the revalidatePath function.
5
5
6
6
`revalidatePath` allows you to purge [cached data](/docs/app/guides/caching) on-demand for a specific path.
7
7
8
+
## Usage
9
+
10
+
`revalidatePath` can be called in Server Functions and Route Handlers.
11
+
12
+
`revalidatePath` cannot be called in Client Components or Middleware, as it only works in server environments.
13
+
8
14
> **Good to know**:
9
15
>
10
-
> -`revalidatePath` only invalidates the cache when the included path is next visited. This means calling `revalidatePath` with a dynamic route segment will not immediately trigger many revalidations at once. The invalidation only happens when the path is next visited.
11
-
> - Currently, `revalidatePath` invalidates all the routes in the [client-side Router Cache](/docs/app/guides/caching#client-side-router-cache) when used in a server action. This behavior is temporary and will be updated in the future to apply only to the specific path.
12
-
> - Using `revalidatePath` invalidates **only the specific path** in the [server-side Route Cache](/docs/app/guides/caching#full-route-cache).
16
+
> -**Server Functions**: Updates the UI immediately (if viewing the revalidated path). Currently, it also causes all previously visited pages to refresh when navigated to again. This behavior is temporary and will be updated in the future to apply only to the specific path.
17
+
> -**Route Handlers**: Marks the path for revalidation. The revalidation is done on the next visit to the specified path. This means calling `revalidatePath` with a dynamic route segment will not immediately trigger many revalidations at once. The invalidation only happens when the path is next visited.
-`path`: Either a string representing the filesystem path associated with the data you want to revalidate (for example, `/product/[slug]/page`), or the literal route segment (for example, `/product/123`). Must be less than 1024 characters. This value is case-sensitive.
25
+
-`path`: Either a string representing the filesystem path associated with the data you want to revalidate (for example, `/product/[slug]/page`), or the literal route segment (for example, `/product/123`). Must not exceed 1024 characters. This value is case-sensitive.
21
26
-`type`: (optional) `'page'` or `'layout'` string to change the type of path to revalidate. If `path` contains a dynamic segment (for example, `/product/[slug]/page`), this parameter is required. If path refers to the literal route segment, e.g., `/product/1` for a dynamic page (e.g., `/product/[slug]/page`), you should not provide `type`.
22
27
23
28
## Returns
24
29
25
30
`revalidatePath` does not return a value.
26
31
32
+
## What can be revalidated
33
+
34
+
The path parameter can point to pages, layouts, or route handlers:
35
+
36
+
-**Pages**: Revalidates the specific page
37
+
-**Layouts**: Revalidates the layout and all pages beneath it
38
+
-**GET Route Handlers**: Only if they're statically generated
39
+
40
+
## Relationship with `revalidateTag`
41
+
42
+
`revalidatePath` and [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) serve different purposes:
43
+
44
+
-**`revalidatePath`**: Revalidates a specific page or layout path
45
+
-**`revalidateTag`**: Revalidates data with specific tags across all pages that use those tags
46
+
47
+
When you call `revalidatePath`, only the specified path gets fresh data on the next visit. Other pages that use the same data tags will continue to serve cached data until those specific tags are also revalidated:
-**Page A (/blog)**: Shows fresh data (page re-rendered)
64
+
-**Page B (/dashboard)**: Still shows stale data (cache tag 'posts' not invalidated)
65
+
66
+
### Building revalidation utilities
67
+
68
+
`revalidatePath` and `revalidateTag` are complementary primitives that are often used together in utility functions to ensure comprehensive data consistency across your application:
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/04-functions/revalidateTag.mdx
+36-8Lines changed: 36 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,17 +5,21 @@ description: API Reference for the revalidateTag function.
5
5
6
6
`revalidateTag` allows you to purge [cached data](/docs/app/guides/caching) on-demand for a specific cache tag.
7
7
8
-
> **Good to know**:
9
-
>
10
-
> -`revalidateTag` only invalidates the cache when the path is next visited. This means calling `revalidateTag` with a dynamic route segment will not immediately trigger many revalidations at once. The invalidation only happens when the path is next visited.
8
+
## Usage
9
+
10
+
`revalidateTag` can be called in Server Functions and Route Handlers.
11
+
12
+
`revalidateTag` cannot be called in Client Components or Middleware, as it only works in server environments.
13
+
14
+
> **Good to know**: `revalidateTag` marks tagged data as stale, but fresh data is only fetched when pages using that tag are next visited. This means calling `revalidateTag` will not immediately trigger many revalidations at once. The invalidation only happens when any page using that tag is next visited.
11
15
12
16
## Parameters
13
17
14
18
```tsx
15
19
revalidateTag(tag: string): void;
16
20
```
17
21
18
-
-`tag`: A string representing the cache tag associated with the data you want to revalidate. Must be less than or equal to 256 characters. This value is case-sensitive.
22
+
-`tag`: A string representing the cache tag associated with the data you want to revalidate. Must not exceed 256 characters. This value is case-sensitive.
`revalidateTag` revalidates data with specific tags across all pages that use those tags, while [`revalidatePath`](/docs/app/api-reference/functions/revalidatePath) revalidates specific page or layout paths.
37
+
38
+
> **Good to know**: These functions serve different purposes and may need to be used together for comprehensive data consistency. For detailed examples and considerations, see [Relationship with revalidateTag](/docs/app/api-reference/functions/revalidatePath#relationship-with-revalidatetag).
39
+
30
40
## Examples
31
41
32
42
### Server Action
@@ -61,8 +71,17 @@ import { revalidateTag } from 'next/cache'
61
71
62
72
exportasyncfunction GET(request:NextRequest) {
63
73
const tag =request.nextUrl.searchParams.get('tag')
0 commit comments