nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Storage mounts with dynamic configuration

Open maou-shonen opened this issue 1 year ago • 6 comments

Describe the feature

relates nuxt/nuxt#20195

The examples in the above discussions are fairly common, maybe provide a function to escape?

Additional information

  • [X] Would you be willing to help implement this feature?

maou-shonen avatar Apr 17 '23 14:04 maou-shonen

Configuration from nitro.config and app.config is serialized during building we cannot use dynamic configuration from there. I am thinking to support server/storage.ts to make it possible or nitro.storage in app.config (however nuxt needs to tree-shake it first)

As a workaround, you can use a runtime plugin to mount in app context using plugins/storage.ts (nuxt: server/plugins/storage.ts)

import redis from "unstorage/drivers/redis";

export default defineNitroPlugin(() => {
  const storage = useStorage();
  storage.mount(
    "/redis",
    redis({
      // ... options from runtime config, app config or process.env
    })
  );
});

pi0 avatar Apr 17 '23 14:04 pi0

If possible, I would still prefer to have some way of use process.env in config. like this nuxt/nuxt#18910 if I have many modules and want to use different default values on different environments, it would provide a better DX , without creating a bunch of plugins.

maou-shonen avatar Apr 17 '23 15:04 maou-shonen

Would it be possible to document the temporary workaround as mentioned by @pi0 (https://github.com/unjs/nitro/issues/1161#issuecomment-1511444675) inside Nuxt documentation?

Would be happy to open a PR for this! Thanks (:

qin-guan avatar Jul 16 '23 10:07 qin-guan

PR also welcome for nitro docs to mention workaround in storage section 👍🏼

pi0 avatar Jul 17 '23 09:07 pi0

Sorry for necro but is there a way to do this for the cache system too? Currently, if I try to mount cache in a plugin using pi0's way, i get ERROR [worker init] already mounted at cache.

Is the only way here to override every single defineCachedEventHandler to use a custom mount point (base)?

daniandl avatar Mar 18 '24 12:03 daniandl

you might need to call .unmount() first.

pi0 avatar Mar 18 '24 13:03 pi0