Add CacheHandler and CacheEntry types to next/cache

Custom cache handler authors currently import CacheHandler and CacheEntry from next/dist/server/lib/cache-handlers/types (an internal path that can move between versions) or hand-copy the interfaces, which drift silently across releases.

The docs even link to the source file on GitHub as the type reference.

Export them as types from the existing next/cache surface, so handlers can be checked against the real interface:

```typescript
  import type { CacheHandler } from 'next/cache'
```

Raised in discussion #96356.
parent cb463843
...@@ -71,7 +71,11 @@ Note that `'use cache: private'` does not use cache handlers and cannot be custo ...@@ -71,7 +71,11 @@ Note that `'use cache: private'` does not use cache handlers and cannot be custo
## API Reference ## API Reference
A cache handler must implement the [`CacheHandler`](https://github.com/vercel/next.js/blob/canary/packages/next/src/server/lib/cache-handlers/types.ts) interface with the following methods: A cache handler must implement the `CacheHandler` interface with the following methods. The `CacheHandler` and `CacheEntry` types are exported from `next/cache`:
```ts
import type { CacheHandler, CacheEntry } from 'next/cache'
```
### `get()` ### `get()`
...@@ -227,7 +231,7 @@ const cacheHandler = { ...@@ -227,7 +231,7 @@ const cacheHandler = {
## CacheEntry Type ## CacheEntry Type
The [`CacheEntry`](https://github.com/vercel/next.js/blob/canary/packages/next/src/server/lib/cache-handlers/types.ts) object has the following structure: The `CacheEntry` object (exported from `next/cache`) has the following structure:
```ts ```ts
interface CacheEntry { interface CacheEntry {
......
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache' export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export type {
CacheHandler,
CacheEntry,
} from './dist/server/lib/cache-handlers/types'
export { export {
revalidatePath, revalidatePath,
revalidateTag, revalidateTag,
......
...@@ -6,7 +6,7 @@ const { AsyncLocalStorage } = require('node:async_hooks') ...@@ -6,7 +6,7 @@ const { AsyncLocalStorage } = require('node:async_hooks')
const snapshot = AsyncLocalStorage.snapshot() const snapshot = AsyncLocalStorage.snapshot()
/** /**
* @type {import('next/dist/server/lib/cache-handlers/types').CacheHandler} * @type {import('next/cache').CacheHandler}
*/ */
const cacheHandler = { const cacheHandler = {
async get(cacheKey, softTags) { async get(cacheKey, softTags) {
......
import type { CacheHandler, CacheEntry } from 'next/cache'
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
;() => {
;({
value: new ReadableStream<Uint8Array>(),
tags: [],
stale: 0,
timestamp: 0,
expire: 0,
revalidate: 0,
}) satisfies CacheEntry
;({
async get(_cacheKey: string, _softTags: string[]) {
return undefined
},
async set(_cacheKey: string, _pendingEntry: Promise<CacheEntry>) {},
async refreshTags() {},
async getExpiration(_tags: string[]) {
return 0
},
async updateTags(_tags: string[], _durations?: { expire?: number }) {},
}) satisfies CacheHandler
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment