Skip to content

Commit

Permalink
feat!: add server cache binding
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Mar 7, 2024
1 parent fae96d4 commit e524bfe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions playground/server/api/cached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default cachedEventHandler(async (event) => {
return {
now: Date.now()
}
}, {
maxAge: 10
})
17 changes: 17 additions & 0 deletions src/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ export default defineNuxtModule<ModuleOptions>({
version
})

// Add Server caching (Nitro)
nuxt.options.nitro = defu(nuxt.options.nitro, {
storage: {
cache: {
driver: 'cloudflare-kv-binding',
binding: 'CACHE',
base: 'cache'
}
},
devStorage: {
cache: {
driver: 'fs',
base: join(rootDir, '.data/cache')
}
}
})

// nuxt prepare or production mode, stop here
if (nuxt.options._prepare || !nuxt.options.dev) {
return
Expand Down
4 changes: 2 additions & 2 deletions src/module/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export function generateWrangler() {
{ binding = "DB", database_name = "default", database_id = "default" },
]
kv_namespaces = [
{ binding = "KV", id = "user_default" },
{ binding = "CONFIG", id = "config_default" },
{ binding = "KV", id = "kv_default" },
{ binding = "CACHE", id = "cache_default" },
]
r2_buckets = [
{ binding = "BLOB", bucket_name = "default" },
Expand Down

0 comments on commit e524bfe

Please sign in to comment.