8000 Always pass implicit/soft tags into the `CacheHandler.get` method (#7… · vercel/next.js@0c0262e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c0262e

Browse files
authored
Always pass implicit/soft tags into the CacheHandler.get method (#79213)
This allows a cache handler to check the timestamp of a cache entry against the latest expiration of the given soft tags, and discarding the entry if it's stale. Subsequently, it does not need to consult a potentially separate tags service to retrieve a value in the `CacheHandler.getExpiration` call for those tags, so that Next.js could handle the expiration check. It can return `Infinity` instead.
1 parent 9832e84 commit 0c0262e

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

.changeset/lovely-bulldogs-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"next": patch
3+
---
4+
5+
Always pass implicit/soft tags into the `CacheHandler.get` method

packages/next/src/server/lib/cache-handlers/types.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,40 @@ export interface CacheHandler {
7777

7878
export interface CacheHandlerV2 {
7979
/**
80-
* Retrieve a cache entry for the given cache key, if available.
80+
* Retrieve a cache entry for the given cache key, if available. Will return
81+
* undefined if there's no valid entry, or if the given soft tags are stale.
8182
*/
82-
get(cacheKey: string): Promise<undefined | CacheEntry>
83+
get(cacheKey: string, softTags: string[]): Promise<undefined | CacheEntry>
8384

8485
/**
8586
* Store a cache entry for the given cache key. When this is called, the entry
8687
* may still be pending, i.e. its value stream may still be written to. So it
87-
* needs to be awaited first. If a `get` for the same cache key is called
88+
* needs to be awaited first. If a `get` for the same cache key is called,
8889
* before the pending entry is complete, the cache handler must wait for the
8990
* `set` operation to finish, before returning the entry, instead of returning
9091
* undefined.
9192
*/
9293
set(cacheKey: string, pendingEntry: Promise<CacheEntry>): Promise<void>
9394

9495
/**
95-
* Next.js will call this method periodically, but always before starting a
96-
* new request. When working with a remote tags service, this method should
97-
* communicate with the tags service to refresh the local tags manifest
98-
* accordingly.
96+
* This function may be called periodically, but always before starting a new
97+
* request. If applicable, it should communicate with the tags service to
98+
* refresh the local tags manifest accordingly.
9999
*/
100100
refreshTags(): Promise<void>
101101

102102
/**
103-
* Next.js will call this method for each set of soft tags that are relevant
104-
* at the start of a request. The result is the maximum timestamp of a
105-
* revalidate event for the tags. Returns `0` if none of the tags were ever
106-
* revalidated.
103+
* This function is called for each set of soft tags that are relevant at the
104+
* start of a request. The result is the maximum timestamp of a revalidate
105+
* event for the tags. Returns `0` if none of the tags were ever revalidated.
106+
* Returns `Infinity` if the soft tags are supposed to be passed into the
107+
* `get` method instead to be checked for expiration.
107108
*/
108109
getExpiration(...tags: string[]): Promise<Timestamp>
109110

110111
/**
111-
* Next.js will call this method when `revalidateTag` or `revalidatePath()` is
112-
* called. It should update the tags manifest accordingly.
112+
* This function is called when tags are revalidated/expired. If applicable,
113+
* it should update the tags manifest accordingly.
113114
*/
114115
expireTags(...tags: string[]): Promise<void>
115116
}

packages/next/src/server/use-cache/use-cache-wrapper.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -744,15 +744,10 @@ export function cache(
744744

745745
let entry = shouldForceRevalidate(workStore, workUnitStore)
746746
? undefined
747-
: 'getExpiration' in cacheHandler
748-
? await cacheHandler.get(serializedCacheKey)
749-
: // Legacy cache handlers require implicit tags to be passed in,
750-
// instead of checking their staleness here, as we do for modern
751-
// cache handlers (see below).
752-
await cacheHandler.get(
753-
serializedCacheKey,
754-
workUnitStore?.implicitTags?.tags ?? []
755-
)
747+
: await cacheHandler.get(
748+
serializedCacheKey,
749+
workUnitStore?.implicitTags?.tags ?? []
750+
)
756751

757752
if (entry) {
758753
const implicitTags = workUnitStore?.implicitTags?.tags ?? []

test/e2e/app-dir/use-cache-custom-handler/handler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const defaultCacheHandler =
77
* @type {import('next/dist/server/lib/cache-handlers/types').CacheHandlerV2}
88
*/
99
const cacheHandler = {
10-
async get(cacheKey) {
11-
console.log('ModernCustomCacheHandler::get', cacheKey)
12-
return defaultCacheHandler.get(cacheKey)
10+
async get(cacheKey, softTags) {
11+
console.log('ModernCustomCacheHandler::get', cacheKey, softTags)
12+
return defaultCacheHandler.get(cacheKey, softTags)
1313
},
1414

1515
async set(cacheKey, pendingEntry) {

test/e2e/app-dir/use-cache-custom-handler/legacy-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const cacheHandler = {
1313
cacheKey,
1414
JSON.stringify(softTags)
1515
)
16-
return defaultCacheHandler.get(cacheKey)
16+
return defaultCacheHandler.get(cacheKey, softTags)
1717
},
1818

1919
async set(cacheKey, pendingEntry) {

test/e2e/app-dir/use-cache-custom-handler/use-cache-custom-handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('use-cache-custom-handler', () => {
2828
expect(cliOutput).toContain('ModernCustomCacheHandler::refreshTags')
2929

3030
expect(next.cliOutput.slice(outputIndex)).toMatch(
31-
/ModernCustomCacheHandler::get \["(development|[A-Za-z0-9_-]{21})","([0-9a-f]{2})+",\[\]\]/
31+
/ModernCustomCacheHandler::get \["(development|[A-Za-z0-9_-]{21})","([0-9a-f]{2})+",\[\]\] \[ '_N_T_\/layout', '_N_T_< 49F4 /span>\/page', '_N_T_\/' \]/
3232
)
3333

3434
expect(next.cliOutput.slice(outputIndex)).toMatch(

0 commit comments

Comments
 (0)
0