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

Add a generic type for unstable_cache #49166

Merged
merged 1 commit into from
May 3, 2023
Merged
Changes from all commits
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
11 changes: 6 additions & 5 deletions packages/next/src/server/web/spec-extension/unstable-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {

type Callback = (...args: any[]) => Promise<any>

// TODO: generic callback type?
export function unstable_cache(
cb: Callback,
export function unstable_cache<T extends Callback>(
cb: T,
keyParts: string[],
options: {
revalidate: number | false
tags?: string[]
}
): Callback {
): T {
const joinedKey = cb.toString() + '-' + keyParts.join(', ')
const staticGenerationAsyncStorage = (
fetch as any
Expand All @@ -34,7 +33,7 @@ export function unstable_cache(
)
}

return async (...args: any[]) => {
const cachedCb = async (...args: any[]) => {
// We override the default fetch cache handling inside of the
// cache callback so that we only cache the specific values returned
// from the callback instead of also caching any fetches done inside
Expand Down Expand Up @@ -131,4 +130,6 @@ export function unstable_cache(
}
)
}
// TODO: once AsyncLocalStorage.run() returns the correct types this override will no longer be necessary
return cachedCb as unknown as T
}