Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuxt 3 - setup Redis using runtime configuration

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.

like image 302
Ellrohir Avatar asked Mar 08 '26 18:03

Ellrohir


1 Answers

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.

like image 89
Ellrohir Avatar answered Mar 11 '26 09:03

Ellrohir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!