Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Configuring redis session #72

Open
gjedlicska opened this issue May 12, 2023 · 4 comments · May be fixed by #90
Open

Configuring redis session #72

gjedlicska opened this issue May 12, 2023 · 4 comments · May be fixed by #90
Labels
question Further information is requested

Comments

@gjedlicska
Copy link

gjedlicska commented May 12, 2023

Ask your question

Should the working example below be the officially recommended way of configuring a nuxt-session?

Additional information

Using the example below based on the docs for configuring the redis session storage is partially broken / misleading.

// file: ~/nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@sidebase/nuxt-session'],
  session: {
    session: {
      storageOptions: {
        driver: 'redis',
        options: {
          url: process.env['REDIS_URL']
        }
      }
    }
  }
})

It is working well in a local dev setup, but breaks, when the app is built into a docker container. Most probably, because at build time, we deliberately do not supply the value of the REDIS_URL env var, the storage driver falls back to its default value.

I've found, that the config is actually read via nuxt's useRuntimeConfig function and supplying the config below, makes it work in both local and prod setups.

export default defineNuxtConfig({
  modules: ['@sidebase/nuxt-session'],
  runtimeConfig: {
    session: {
      session: {
        storageOptions: {
          driver: 'redis',
          options: {
            url: () => process.env['REDIS_URL']
          }
        }
      }
    }
  }
})
@gjedlicska gjedlicska added the question Further information is requested label May 12, 2023
@odranoelBR
Copy link

odranoelBR commented Jun 21, 2023

Hi @gjedlicska, i tried this runtimeConfig for build production and didn't work.
My attempt commands:

yarn build
export REDIS_URL=redis://localhost:6380 (intentional changed port)
yarn preview

He don't log the error message "can't connect ..." but save nothing on redis.

Tks

@luukgruijs
Copy link

@gjedlicska i created this issue two weeks ago: #82 . I can't seem to figure this out. Would you have any ideas?

@gjedlicska
Copy link
Author

gjedlicska commented Jul 17, 2023

@luukgruijs
Turns out, if you only set the env var NUXT_SESSION_SESSION_STORAGE_OPTIONS_OPTIONS_URL , what should be the full url of the redis instance, you do not need to configure anything to your nuxt.config.ts file.

That's what I've started using.

@gjedlicska
Copy link
Author

To provide a full example for anyone still struggling with this:

{
  modules: ['@sidebase/nuxt-session'],
  // the build time config is needed, so that the nuxt module selects the right driver module during build 
  session: {
    session: {
      storageOptions: {
        driver: 'redis',
        options: {
        }
      }
    }
  },
  // the runtime config is needed, to specify the shape of the config object.
  // a dummy value for the url, that can be overridden by nuxt's runtime env var system
  // https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables
  runtimeConfig: {
    session: {
      session: {
        storageOptions: {
          driver: 'redis',
          options: {
            // overriden by NUXT_SESSION_SESSION_STORAGE_OPTIONS_OPTIONS_URL env var
            url: 'UNDEFINED' // you need to define the key, with a dummy value
          }
        }
      }
    }
  }
}

I'm adding a PR to update the readme with this.

@gjedlicska gjedlicska reopened this Nov 1, 2023
@gjedlicska gjedlicska linked a pull request Nov 1, 2023 that will close this issue
4 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants