-
Notifications
You must be signed in to change notification settings - Fork 11
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: use the cache api if there is no kv cache available #136
base: main
Are you sure you want to change the base?
Changes from all 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]) { | ||
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. I think we should remove the bindings from 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. getCloudflareContext is async at the moment because of local dev, so it won't work here unless we move the retrieving of the cache to be part of an interaction with the cache instead of when the cache handler is instantiated. 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. yep, you can create a TODO/issue for that. thanks. 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. |
||
return new KVStore(process.env[kvName] as unknown as KVNamespace); | ||
} | ||
|
||
return new CacheAPIStore(); | ||
} | ||
|
||
const oneYearInMs = 31536000; | ||
|
||
class KVStore implements CacheStore { | ||
constructor(private store: KVNamespace) {} | ||
|
||
get(key: string) { | ||
return this.store.get<CacheEntry>(key, "json"); | ||
} | ||
|
||
put(key: string, entry: CacheEntry, ttl = oneYearInMs) { | ||
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 = oneYearInMs) { | ||
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}`; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are - the return type is a promise