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

feat(cache): add staleMaxAge option for caching header #116

Merged
merged 7 commits into from
Apr 19, 2022
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
25 changes: 15 additions & 10 deletions src/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ export interface CacheEntry<T=any> {
integrity?: string
}

export interface CachifyOptions<T=any> {
name?: string
getKey?: (...args: any[]) => string
transform?: (entry: CacheEntry<T>, ...args: any[]) => any
group?: string
integrity?: any
magAge?: number
swr?: boolean
base?: string
export interface CachifyOptions<T = any> {
name?: string;
getKey?: (...args: any[]) => string;
transform?: (entry: CacheEntry<T>, ...args: any[]) => any;
group?: string;
integrity?: any;
magAge?: number;
swr?: boolean;
staleMaxAge?: number;
base?: string;
}

const defaultCacheOptions = {
Expand Down Expand Up @@ -135,7 +136,11 @@ export function defineCachedEventHandler (handler: CompatibilityEventHandler, op
if (opts.magAge) {
cacheControl.push(`s-maxage=${opts.magAge}`)
}
cacheControl.push('stale-while-revalidate')
if (opts.staleMaxAge) {
cacheControl.push(`stale-while-revalidate=${opts.staleMaxAge}`)
} else {
cacheControl.push('stale-while-revalidate')
}
} else if (opts.magAge) {
cacheControl.push(`max-age=${opts.magAge}`)
}
Expand Down