Unverified Commit 46709a2a authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

docs: update revalidatePath, revalidateTag docs to be more consistent +…

docs: update revalidatePath, revalidateTag docs to be more consistent + highlight res.revalidate case (#82479)

- uses invalidate instead of revalidate to make more explicit the
revalidatePath, revalidateTag behavior
- highlights that eager revalidation can still happen with the pages
router method

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent e590df4b
......@@ -281,9 +281,11 @@ We recommend setting a high revalidation time. For instance, 1 hour instead of 1
### On-demand revalidation with `revalidatePath`
For a more precise method of revalidation, invalidate pages on-demand with the `revalidatePath` function.
For a more precise method of revalidation, invalidate cached pages on-demand with the `revalidatePath` function.
For example, this Server Action would get called after adding a new post. Regardless of how you retrieve your data in your Server Component, either using `fetch` or connecting to a database, this will immediately clear the cache for the entire route. Unlike time-based revalidation, the next request after on-demand revalidation won't return stale data but will serve the updated version right away and cache it for subsequent requests.
For example, this Server Action would get called after adding a new post. Regardless of how you retrieve your data in your Server Component, either using `fetch` or connecting to a database, this will invalidate the cache for the entire route. The next request to that route will trigger regeneration and serve fresh data, which will then be cached for subsequent requests.
> **Note:** `revalidatePath` invalidates the cache entries but regeneration happens on the next request. If you want to eagerly regenerate the cache entry immediately instead of waiting for the next request, you can use the Pages router [`res.revalidate`](docs/pages/guides/incremental-static-regeneration#on-demand-validation-with-resrevalidate) method. We're working on adding new methods to provide eager regeneration capabilities for the App Router.
```ts filename="app/actions.ts" switcher
'use server'
......@@ -291,7 +293,7 @@ For example, this Server Action would get called after adding a new post. Regard
import { revalidatePath } from 'next/cache'
export async function createPost() {
// Invalidate the /posts route in the cache
// Invalidate the cache for the /posts route
revalidatePath('/posts')
}
```
......@@ -302,7 +304,7 @@ export async function createPost() {
import { revalidatePath } from 'next/cache'
export async function createPost() {
// Invalidate the /posts route in the cache
// Invalidate the cache for the /posts route
revalidatePath('/posts')
}
```
......@@ -379,7 +381,7 @@ You can then use `revalidateTag` in a [Server Actions](/docs/app/getting-started
import { revalidateTag } from 'next/cache'
export async function createPost() {
// Invalidate all data tagged with 'posts' in the cache
// Invalidate all data tagged with 'posts'
revalidateTag('posts')
}
```
......@@ -390,7 +392,7 @@ export async function createPost() {
import { revalidateTag } from 'next/cache'
export async function createPost() {
// Invalidate all data tagged with 'posts' in the cache
// Invalidate all data tagged with 'posts'
revalidateTag('posts')
}
```
......
......@@ -3,7 +3,7 @@ title: revalidatePath
description: API Reference for the revalidatePath function.
---
`revalidatePath` allows you to purge [cached data](/docs/app/guides/caching) on-demand for a specific path.
`revalidatePath` allows you to invalidate [cached data](/docs/app/guides/caching) on-demand for a specific path.
## Usage
......@@ -29,13 +29,13 @@ revalidatePath(path: string, type?: 'page' | 'layout'): void;
`revalidatePath` does not return a value.
## What can be revalidated
## What can be invalidated
The path parameter can point to pages, layouts, or route handlers:
- **Pages**: Revalidates the specific page
- **Layouts**: Revalidates the layout and all pages beneath it
- **Route Handlers**: Revalidates Data Cache entries accessed within route handlers. For example `revalidatePath("/api/data")` revalidates this GET handler:
- **Pages**: Invalidates the specific page
- **Layouts**: Invalidates the layout and all pages beneath it
- **Route Handlers**: Invalidates Data Cache entries accessed within route handlers. For example `revalidatePath("/api/data")` invalidates this GET handler:
```ts filename="app/api/data/route.ts"
export async function GET() {
......@@ -51,8 +51,8 @@ export async function GET() {
`revalidatePath` and [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) serve different purposes:
- **`revalidatePath`**: Revalidates a specific page or layout path
- **`revalidateTag`**: Revalidates data with specific tags across all pages that use those tags
- **`revalidatePath`**: Invalidates a specific page or layout path
- **`revalidateTag`**: Invalidates data with specific tags across all pages that use those tags
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:
......@@ -101,7 +101,7 @@ import { revalidatePath } from 'next/cache'
revalidatePath('/blog/post-1')
```
This will revalidate one specific URL on the next page visit.
This will invalidate one specific URL for revalidation on the next page visit.
### Revalidating a Page path
......@@ -112,7 +112,7 @@ revalidatePath('/blog/[slug]', 'page')
revalidatePath('/(main)/blog/[slug]', 'page')
```
This will revalidate any URL that matches the provided `page` file on the next page visit. This will _not_ invalidate pages beneath the specific page. For example, `/blog/[slug]` won't invalidate `/blog/[slug]/[author]`.
This will invalidate any URL that matches the provided `page` file for revalidation on the next page visit. This will _not_ invalidate pages beneath the specific page. For example, `/blog/[slug]` won't invalidate `/blog/[slug]/[author]`.
### Revalidating a Layout path
......@@ -123,7 +123,7 @@ revalidatePath('/blog/[slug]', 'layout')
revalidatePath('/(main)/post/[slug]', 'layout')
```
This will revalidate any URL that matches the provided `layout` file on the next page visit. This will cause pages beneath with the same layout to revalidate on the next visit. For example, in the above case, `/blog/[slug]/[another]` would also revalidate on the next visit.
This will invalidate any URL that matches the provided `layout` file for revalidation on the next page visit. This will cause pages beneath with the same layout to be invalidated and revalidated on the next visit. For example, in the above case, `/blog/[slug]/[another]` would also be invalidated and revalidated on the next visit.
### Revalidating all data
......@@ -133,7 +133,7 @@ import { revalidatePath } from 'next/cache'
revalidatePath('/', 'layout')
```
This will purge the Client-side Router Cache, and revalidate the Data Cache on the next page visit.
This will purge the Client-side Router Cache, and invalidate the Data Cache for revalidation on the next page visit.
### Server Function
......
......@@ -3,7 +3,7 @@ title: revalidateTag
description: API Reference for the revalidateTag function.
---
`revalidateTag` allows you to purge [cached data](/docs/app/guides/caching) on-demand for a specific cache tag.
`revalidateTag` allows you to invalidate [cached data](/docs/app/guides/caching) on-demand for a specific cache tag.
## Usage
......@@ -33,7 +33,7 @@ fetch(url, { next: { tags: [...] } });
## Relationship with `revalidatePath`
`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.
`revalidateTag` invalidates data with specific tags across all pages that use those tags, while [`revalidatePath`](/docs/app/api-reference/functions/revalidatePath) invalidates specific page or layout paths.
> **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).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment