Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tags limit handling for max items #63486

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/02-app/02-api-reference/04-functions/fetch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Set the cache lifetime of a resource (in seconds).
fetch(`https://...`, { next: { tags: ['collection'] } })
```

Set the cache tags of a resource. Data can then be revalidated on-demand using [`revalidateTag`](https://nextjs.org/docs/app/api-reference/functions/revalidateTag). The max length for a custom tag is 256 characters.
Set the cache tags of a resource. Data can then be revalidated on-demand using [`revalidateTag`](https://nextjs.org/docs/app/api-reference/functions/revalidateTag). The max length for a custom tag is 256 characters and the max tag items is 64.

## Version History

Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const NEXT_CACHE_REVALIDATED_TAGS_HEADER = 'x-next-revalidated-tags'
export const NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER =
'x-next-revalidate-tag-token'

// if these change make sure we update the related
// documentation as well
export const NEXT_CACHE_TAG_MAX_ITEMS = 64
export const NEXT_CACHE_TAG_MAX_LENGTH = 256
export const NEXT_CACHE_SOFT_TAG_MAX_LENGTH = 1024
export const NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_'
Expand Down
13 changes: 12 additions & 1 deletion packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getTracer, SpanKind } from './trace/tracer'
import {
CACHE_ONE_YEAR,
NEXT_CACHE_IMPLICIT_TAG_ID,
NEXT_CACHE_TAG_MAX_ITEMS,
NEXT_CACHE_TAG_MAX_LENGTH,
} from '../../lib/constants'
import * as Log from '../../build/output/log'
Expand Down Expand Up @@ -53,7 +54,9 @@ export function validateTags(tags: any[], description: string) {
reason: string
}> = []

for (const tag of tags) {
for (let i = 0; i < tags.length; i++) {
const tag = tags[i]

if (typeof tag !== 'string') {
invalidTags.push({ tag, reason: 'invalid type, must be a string' })
} else if (tag.length > NEXT_CACHE_TAG_MAX_LENGTH) {
Expand All @@ -64,6 +67,14 @@ export function validateTags(tags: any[], description: string) {
} else {
validTags.push(tag)
}

if (validTags.length >= NEXT_CACHE_TAG_MAX_ITEMS) {
ijjk marked this conversation as resolved.
Show resolved Hide resolved
console.warn(
`Warning: exceeded max tag count for ${description}, dropped tags:`,
tags.slice(i, tags.length).join(', ')
ijjk marked this conversation as resolved.
Show resolved Hide resolved
)
break
}
}

if (invalidTags.length > 0) {
Expand Down
Loading