revalidatePath
revalidatePath
允许你按需清除特定路径的 缓存数据。
¥revalidatePath
allows you to purge cached data on-demand for a specific path.
很高兴知道:
¥Good to know:
revalidatePath
在 Node.js 和 Edge 运行时 中均可用。¥
revalidatePath
is available in both Node.js and Edge runtimes.
revalidatePath
仅在下次访问包含的路径时使缓存无效。这意味着使用动态路由段调用revalidatePath
不会立即触发多次重新验证。仅当下次访问该路径时才会发生失效。¥
revalidatePath
only invalidates the cache when the included path is next visited. This means callingrevalidatePath
with a dynamic route segment will not immediately trigger many revalidations at once. The invalidation only happens when the path is next visited.目前,
revalidatePath
使 客户端路由缓存 中的所有路由失效。此行为是临时的,将来会更新以仅适用于特定路径。¥Currently,
revalidatePath
invalidates all the routes in the client-side Router Cache. This behavior is temporary and will be updated in the future to apply only to the specific path.使用
revalidatePath
只会使 服务器端路由缓存 中的特定路径无效。¥Using
revalidatePath
invalidates only the specific path in the server-side Route Cache.
参数
¥Parameters
revalidatePath(path: string, type?: 'page' | 'layout'): void;
-
path
:表示与要重新验证的数据关联的文件系统路径的字符串(例如,/product/[slug]/page
),或字面量路由段(例如,/product/123
)。必须少于 1024 个字符。该值区分大小写。¥
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. -
type
:(可选)'page'
或'layout'
字符串更改要重新验证的路径类型。如果path
包含动态段(例如/product/[slug]/page
),则需要此参数。如果路径指的是文字路由段,例如动态页面的/product/1
(例如/product/[slug]/page
),则不应提供type
。¥
type
: (optional)'page'
or'layout'
string to change the type of path to revalidate. Ifpath
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 providetype
.
返回
¥Returns
revalidatePath
不返回值。
¥revalidatePath
does not return a value.
示例
¥Examples
重新验证特定 URL
¥Revalidating A Specific URL
import { revalidatePath } from 'next/cache'
revalidatePath('/blog/post-1')
这将在下一页访问时重新验证一个特定的 URL。
¥This will revalidate one specific URL on the next page visit.
重新验证页面路径
¥Revalidating A Page Path
import { revalidatePath } from 'next/cache'
revalidatePath('/blog/[slug]', 'page')
// or with route groups
revalidatePath('/(main)/blog/[slug]', 'page')
这将在下一个页面访问时重新验证与所提供的 page
文件匹配的所有 URL。这不会使特定页面下方的页面失效。例如,/blog/[slug]
不会使 /blog/[slug]/[author]
无效。
¥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]
.
重新验证布局路径
¥Revalidating A Layout Path
import { revalidatePath } from 'next/cache'
revalidatePath('/blog/[slug]', 'layout')
// or with route groups
revalidatePath('/(main)/post/[slug]', 'layout')
这将在下一个页面访问时重新验证与所提供的 layout
文件匹配的所有 URL。这将导致具有相同布局的下方页面在下次访问时重新验证。例如,在上述情况下,/blog/[slug]/[another]
也会在下次访问时重新验证。
¥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.
重新验证所有数据
¥Revalidating All Data
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.
服务器动作
¥Server Action
'use server'
import { revalidatePath } from 'next/cache'
export default async function submit() {
await submitForm()
revalidatePath('/')
}
路由处理程序
¥Route Handler
import { revalidatePath } from 'next/cache'
import type { NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
const path = request.nextUrl.searchParams.get('path')
if (path) {
revalidatePath(path)
return Response.json({ revalidated: true, now: Date.now() })
}
return Response.json({
revalidated: false,
now: Date.now(),
message: 'Missing path to revalidate',
})
}
import { revalidatePath } from 'next/cache'
export async function GET(request) {
const path = request.nextUrl.searchParams.get('path')
if (path) {
revalidatePath(path)
return Response.json({ revalidated: true, now: Date.now() })
}
return Response.json({
revalidated: false,
now: Date.now(),
message: 'Missing path to revalidate',
})
}