Skip to content

How to setup cache storage to change in runtime ? #2944

Answered by cjpearson
geminigeek asked this question in Q&A
Discussion options

You must be logged in to vote

Instead of defining the storage in the nuxt.config, you can create a nitro plugin which mounts a storage based on some runtime configuration.

#1161 (comment)

Something like this:

import redis from "unstorage/drivers/redis";

export default defineNitroPlugin(() => {
  const storage = useStorage();
  const config = useRuntimeConfig();
  storage.mount(
    "/redis",
    redis({
      url: config.redisUrl
    })
  );
});
export default defineNuxtConfig({
  runtimeConfig: {
    redisUrl: '' // set in .env with NUXT_REDIS_URL
  },
  routeRules: {
    "/blog/**": { cache: { maxAge: 60 * 60, base: "redis" } },
  },
});

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@geminigeek
Comment options

Answer selected by geminigeek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants