N
Nuxt3mo ago
CedsTrash

defineCachedEventHandler - set maxAge from environment variable

Hi everyone, I'm trying to figure out how to set my maxAge value from my config because I want it to be different in staging vs production:
export default defineCachedEventHandler(async (event) => {
const config = useRuntimeConfig()

const body = await readBody(event)

let url = config.public.BASE_API_URL + '/collections/' + body.type + '/entries?filter[site]=' + body.locale + '&filter[url]=' + body.url
if ('previewToken' in body) {
url += '&token=' + body.previewToken
}
const result = await $fetch(url)

return {
data: result.data,
fetchedAt: new Date()
}
}, {
maxAge: 1, // Change back to 30
swr: false,
shouldBypassCache: async (event) => {
const body = await readBody(event)
return 'previewToken' in body
},
getKey: async (event) => {
const body = await readBody(event)

return body.cacheKey
}
})
export default defineCachedEventHandler(async (event) => {
const config = useRuntimeConfig()

const body = await readBody(event)

let url = config.public.BASE_API_URL + '/collections/' + body.type + '/entries?filter[site]=' + body.locale + '&filter[url]=' + body.url
if ('previewToken' in body) {
url += '&token=' + body.previewToken
}
const result = await $fetch(url)

return {
data: result.data,
fetchedAt: new Date()
}
}, {
maxAge: 1, // Change back to 30
swr: false,
shouldBypassCache: async (event) => {
const body = await readBody(event)
return 'previewToken' in body
},
getKey: async (event) => {
const body = await readBody(event)

return body.cacheKey
}
})
I would like to set maxAge to config.NITRO_CACHE_TTL but since it expects a number I can't use a function. Would someone know how to do achieve this? Thanks.
7 Replies
saltytostitos
saltytostitos3mo ago
I often use a ternary here (everybody's absolute favorite).
maxAge: import.meta.dev ? config?.NITRO_CACHE_TTL : 1
maxAge: import.meta.dev ? config?.NITRO_CACHE_TTL : 1
typecast if needed:
Number(config?.NITRO_CACHE_TTL) || 1
Number(config?.NITRO_CACHE_TTL) || 1
Or you can use an arrow function
maxAge: () => { return some_logic_as_a_number }
maxAge: () => { return some_logic_as_a_number }
CedsTrash
CedsTrashOP3mo ago
@saltytostitos Thanks for your answer. But I'm getting an error message as soon as I use an arrow function instead of a number
No description
CedsTrash
CedsTrashOP3mo ago
I've tried the following:
maxAge: () => { return 1 },
maxAge: () => {
const config = useRuntimeConfig()
return Number(config?.NITRO_CACHE_TTL) || 1
},
maxAge: () => { return 1 },
maxAge: () => {
const config = useRuntimeConfig()
return Number(config?.NITRO_CACHE_TTL) || 1
},
maxAge: import.meta.dev ? config?.NITRO_CACHE_TTL : 1 doesn't work because config isn't defined.
Cue
Cue3mo ago
@CedsTrash is NITRO_CACHE_TTL coming from an env file?
CedsTrash
CedsTrashOP3mo ago
Yes!
Cue
Cue3mo ago
Then you can use import.meta.env or process.env instead of runtimeConfig
CedsTrash
CedsTrashOP3mo ago
I got it to work like this: maxAge: Number(process.env.NITRO_CACHE_TTL) || 1, Arrow function just don't work. Thanks a lot for your help!
Want results from more Discord servers?
Add your server