-
Notifications
You must be signed in to change notification settings - Fork 73
feat: use the cache api if there is no kv cache available #136
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@opennextjs/cloudflare": minor | ||
--- | ||
|
||
feat: use the cache api if there is no kv cache available | ||
|
||
Instead of requiring a KV cache is available in the environment for Next.js caching to work, the cache handle will default to using the Cache API. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { IncrementalCacheValue } from "next/dist/server/response-cache"; | ||
|
||
export type CacheEntry = { | ||
lastModified: number; | ||
value: IncrementalCacheValue | null; | ||
}; | ||
|
||
export type CacheStore = { | ||
get: (key: string) => Promise<CacheEntry | null>; | ||
put: (key: string, entry: CacheEntry, ttl?: number) => Promise<void>; | ||
}; | ||
|
||
export function getCacheStore() { | ||
const kvName = process.env.__OPENNEXT_KV_BINDING_NAME; | ||
if (process.env[kvName]) { | ||
dario-piotrowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new KVStore(process.env[kvName] as unknown as KVNamespace); | ||
} | ||
|
||
return new CacheAPIStore(); | ||
} | ||
|
||
const defaultTTL = 31536000; // 1 year | ||
james-elicx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
class KVStore implements CacheStore { | ||
constructor(private store: KVNamespace) {} | ||
|
||
get(key: string) { | ||
return this.store.get<CacheEntry>(key, "json"); | ||
} | ||
|
||
put(key: string, entry: CacheEntry, ttl = defaultTTL) { | ||
return this.store.put(key, JSON.stringify(entry), { | ||
expirationTtl: ttl, | ||
}); | ||
} | ||
} | ||
|
||
class CacheAPIStore implements CacheStore { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @petebacondarwin @dario-piotrowicz I'm not familiar the cf cache API, do you have thoughts about this class? With my limited understanding of the cache API, I'm not sure if we should use it for the fetch cache (as long as the cache headers are set correctly on the I'm also wondering about custom keys. |
||
constructor(private name = "__opennext_cache") {} | ||
|
||
async get(key: string) { | ||
const cache = await caches.open(this.name); | ||
const response = await cache.match(this.createCacheKey(key)); | ||
|
||
if (response) { | ||
return response.json<CacheEntry>(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async put(key: string, entry: CacheEntry, ttl = defaultTTL) { | ||
const cache = await caches.open(this.name); | ||
|
||
const response = new Response(JSON.stringify(entry), { | ||
headers: { "cache-control": `max-age=${ttl}` }, | ||
}); | ||
|
||
return cache.put(this.createCacheKey(key), response); | ||
} | ||
|
||
private createCacheKey(key: string) { | ||
return `https://${this.name}.local/entry/${key}`; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.