Using Nuxt 3.3.2 I have Redis integration implemented as shown in Nuxt 3 docs: https://nuxt.com/docs/guide/directory-structure/server#example-using-redis
It works, only the connection is hard-coded in nuxt.config.ts. Is it possible to make it configurable using Nuxt runtime config variables?
I managed to this:
redis: {
driver: 'redis',
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASS
}
but those are underlying system env variables, not the Nuxt ones. So it is an option, but not the ideal one.
I also tried useRuntimeConfig(), but got error it is undefined when used inside nuxt.config.ts
The question was originally asked at Nuxt GitHub forum, but no answer yet.
Answered here by one of the Nuxt collaborators.
The key is to define Nuxt server plugin in server/plugins directory:
import redis from "unstorage/drivers/redis";
export default defineNitroPlugin(() => {
const storage = useStorage();
storage.mount(
"/redis",
redis({
// useRuntimeConfig() is available here
})
);
});
According to the discussion, more convenient way might be implemented into Nuxt eventually. But so far this "workaround" gets the job done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With