Skip to content

Commit

Permalink
different approach: add derived tags for urlpathname
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed May 6, 2024
1 parent 4850cc7 commit 995546f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ revalidatePath(path: string, type?: 'page' | 'layout'): void;
```

- `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`: (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 the path references a specific dynamic segment, ie `/product/1`, `type` will be ignored.
- `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.

## Returns

Expand Down
18 changes: 14 additions & 4 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import * as Log from '../../build/output/log'
import { trackDynamicFetch } from '../app-render/dynamic-rendering'
import type { FetchMetric } from '../base-http'
import { addPathSuffix } from '../../shared/lib/router/utils/add-path-suffix'

const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'

Expand Down Expand Up @@ -149,12 +150,21 @@ export function addImplicitTags(staticGenerationStore: StaticGenerationStore) {

if (urlPathname) {
const parsedPathname = new URL(urlPathname, 'http://n').pathname
const derivedTags = [
// for the canonical browser URL, we still want to add derived tags so that it's possible
// to call `revalidatePath("/some/dynamic/path", "page")` and have it revalidate the page
...getDerivedTags(addPathSuffix(parsedPathname, '/page')),
// we also add the pathname itself for when "page" / "layout" aren't provided
parsedPathname,
]

const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${parsedPathname}`
if (!staticGenerationStore.tags?.includes(tag)) {
staticGenerationStore.tags.push(tag)
for (let tag of derivedTags) {
tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${tag}`
if (!staticGenerationStore.tags?.includes(tag)) {
staticGenerationStore.tags.push(tag)
}
newTags.push(tag)
}
newTags.push(tag)
}
return newTags
}
Expand Down
21 changes: 6 additions & 15 deletions packages/next/src/server/web/spec-extension/revalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,12 @@ export function revalidatePath(originalPath: string, type?: 'layout' | 'page') {

let normalizedPath = `${NEXT_CACHE_IMPLICIT_TAG_ID}${originalPath}`

// A static route should not have a type, because a cache tag belonging to a dynamic
// page won't ever be tagged with `/page` or `/layout` -- it'd just be the canonical URL.
// We only append the type in the dynamic case, or for the root layout.
if (isDynamicRoute(originalPath)) {
if (type) {
normalizedPath += `${normalizedPath.endsWith('/') ? '' : '/'}${type}`
} else {
console.warn(
`Warning: a dynamic page path "${originalPath}" was passed to "revalidatePath", but the "type" parameter is missing. This has no effect by default, see more info here https://nextjs.org/docs/app/api-reference/functions/revalidatePath`
)
}
} else if (normalizedPath === '/' && type === 'layout') {
// the only time we want to append "type" is if it's a root layout revalidation,
// as all cached fetches are tagged with `/layout`.
normalizedPath = '/layout'
if (type) {
normalizedPath += `${normalizedPath.endsWith('/') ? '' : '/'}${type}`
} else if (isDynamicRoute(originalPath)) {
console.warn(
`Warning: a dynamic page path "${originalPath}" was passed to "revalidatePath", but the "type" parameter is missing. This has no effect by default, see more info here https://nextjs.org/docs/app/api-reference/functions/revalidatePath`
)
}
return revalidate(normalizedPath, `revalidatePath ${originalPath}`)
}
Expand Down

0 comments on commit 995546f

Please sign in to comment.