Open gjedlicska opened 1 year ago
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
@gjedlicska i created this issue two weeks ago: #82 . I can't seem to figure this out. Would you have any ideas?
@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.
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.
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.
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.