image icon indicating copy to clipboard operation
image copied to clipboard

Ability to add version for cloudinary provider

Open Davounet opened this issue 5 years ago • 3 comments

Hello, Thank you very much for this module ! I am using it with cloudinary as a provider, but I would like to set the version in the url (the timestamp used to bypass cache issues). I can not include it in the baseURL since it comes after the transformation string. Is there anything planned to address this ? Thank you again

Davounet avatar Jun 01 '21 12:06 Davounet

@Davounet You could create your own custom provider, something like this:

export const getImage = (src, { modifiers = {}, baseURL = '/' } = {}, { $img }) => {
  const url = $img.cloudinary(src, { modifiers, baseURL })

  return {
    url: url + '?timestamp=' + Date.now()
  }
}

danielroe avatar Jun 01 '21 12:06 danielroe

@danielroe Thank you for your answer I'm trying with your solution, but I can't access the existing cloudinary helper inside $img I am trying to find it somewhere in the module, but without success for now. I'll keep trying ;)

Davounet avatar Jun 01 '21 15:06 Davounet

I got it working with the following :

Inside nuxt.config.js

image: {
    provider: 'custom',
    providers: {
      custom: {
        provider: '~/providers/custom',
        options: {
          version: `v${Math.round(Date.now() / 1000)}`
        }
      }
    },
    cloudinary: {
      baseURL: 'https://res.cloudinary.com/numorning/image/upload/'
    }
  }

the custom.js provider

export function getImage(src, { modifiers = {} }, { options }) {
  const baseURL = options.providers.cloudinary.defaults.baseURL
  const { url: base } = options.providers.cloudinary.provider.getImage('/', { modifiers, baseURL })

  const version = options.providers.custom.defaults.version
  const url = `${base}/${version}/${src}`
  return { url }
}

Thank you again !

Davounet avatar Jun 02 '21 06:06 Davounet