nuxt-modules / og-image

Generate OG Images with Vue templates in Nuxt.
https://nuxtseo.com/og-image
417 stars 27 forks source link

Support dynamic `runtimeCacheStorage` #179

Open tberk opened 8 months ago

tberk commented 8 months ago

Hello currently I set runtime cache storage as so:

ogImage: {   
    runtimeCacheStorage: {
      driver: 'redis',
      host: process.env.NUXT_REDIS_HOST,
      port: 6379,
      ttlSeconds: 60 * 60 * 24 * 3,
      base: 'best-og',
      password: process.env.NUXT_REDIS_PASSWORD,
    },
  }

I also have a nitro plugin to register redis storage (Independent of og:image config)

export default defineNitroPlugin(() => {
  const storage = useStorage()
  const config = useRuntimeConfig()

  const driver = redisDriver({
    base: config.redis.base,
    host: config.redis.host,
    port: 6379,
    ttl: 60 * 60 * 24 * 3, // 3 days
    password: config.redis.password,
  })

  // Mount driver
  storage.mount('redis', driver)

  // https://nitro.unjs.io/guide/cache
  // Use redis on cache instead of memory
  await storage.unmount('cache')
  storage.mount('cache', driver)
})

Module works fine with above config however I was wondering if there is a better way of doing it.

harlan-zw commented 4 months ago

Hi, thanks for the issue (sorry for the delay). This is a great question.

Technically you should just be able to do storage.mount('/cache/nuxt-og-image', driver) (the default cache path) within your runtime config and omit runtimeCacheStorage if you want to configure it like that.

Is there any particular reason you're creating the driver yourself instead of just doing it in the config though?

tberk commented 4 months ago

Hello, thanks for the answer.

The reason is the same actually, to get the runtime config when creating the driver. Here is the reference: https://nitro.unjs.io/guide/storage#runtime-configuration