revalidatePath and Implicit Tags
Hard · Caching
revalidatePath(path, type?) marks cached pages stale so they are re-rendered on the next visit. It works through tags that Next.js adds automatically to every cached page (implicit tags). This is a simplified model of that mechanism.
For a cached page at URL url rendered by the file file (for example app/(store)/shop/[item]/page.tsx), take the route path: the file without the leading app and without the extension, so /(store)/shop/[item]/page. Route groups stay in it. The page has these tags:
- /layout
- for every prefix of the route path: the prefix + /layout, except the full path ending in /page, which is used as-is. For /(store)/shop/[item]/page that is /(store)/layout, /(store)/shop/layout, /(store)/shop/[item]/layout, /(store)/shop/[item]/page.
- the URL itself, e.g. /shop/hat.
revalidatePath(path, type) builds one tag: path alone when there is no type, otherwise path + "/" + type (no extra slash when path is /, so ("/", "layout") gives /layout). Every entry with that tag becomes stale. This explains the documented behaviour:
- revalidatePath("/blog/a") refreshes just that URL.
- revalidatePath("/blog/[slug]", "page") refreshes every page of that route file, but not nested routes.
- revalidatePath("/blog", "layout") refreshes everything under /blog; ("/", "layout") refreshes everything.
- A literal path with a type (("/blog/a", "page")) matches nothing for a dynamic route, and route groups must be included in the path ("/(store)/shop").
Write staleAfter(entries, ops). All entries start fresh. ops are ["revalidatePath", path, type?] or ["visit", url] (the page is re-rendered and becomes fresh again; unknown URLs are ignored). Return the stale URLs sorted with sort().