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:
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
I often use a ternary here (everybody's absolute favorite).
typecast if needed:
Or you can use an arrow function
@saltytostitos Thanks for your answer. But I'm getting an error message as soon as I use an arrow function instead of a number
I've tried the following:
maxAge: import.meta.dev ? config?.NITRO_CACHE_TTL : 1
doesn't work because config
isn't defined.@CedsTrash is NITRO_CACHE_TTL coming from an env file?
Yes!
Then you can use import.meta.env or process.env instead of runtimeConfig
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!