image icon indicating copy to clipboard operation
image copied to clipboard

Docs: Custom provider docs outdated?

Open NetMan134 opened this issue 8 months ago • 3 comments

Hi! I'm using NuxtImg, and (want to) use a custom provider (for my very simple use case for now - an nginx server with images and video)

I ran npx nuxi module add @nuxt/image and tried to add a custom provider

Now, I stumbled upon issues trying to follow instructions under this link: https://image.nuxt.com/advanced/custom-provider

The example presented in the link above does not work - the main problem seems to be the lack of defineProvider, it is not exported - does not even exist in the node_modules/@nuxt/image anywhere (but does exist on this repo in the src dir, which is not present after installing @nuxt/image with nuxi or npm for that matter) - and so the app refuses to run

I scoured the web for solutions, and I stumbled upon this discussion here, which hopefully points or pointed me already to the right direction:

providers/selfhost.ts

import { joinURL } from 'ufo';
import type { ProviderGetImage } from '@nuxt/image';
import { createOperationsGenerator } from '#image';

const operationsGenerator = createOperationsGenerator();

export const getImage: ProviderGetImage = (
  src,
  { modifiers = {}, baseUrl } = {},
) => {
  if (!baseUrl) {
    baseUrl = '';
  }

  const operations = operationsGenerator(modifiers);

  return {
    url: joinURL(baseUrl, src + (operations ? '?' + operations : '')),
  };
};

nuxt.config.ts

export default defineNuxtConfig({
  modules: [
    '@nuxt/image'
  ],

  (...)

  image: {
    providers: {
      selfhost: {
        name: 'selfhost',
        provider: '~/providers/selfhost.ts',
        options: {
          baseUrl: 'http://MY_URL.TLD/',
        },
      },
    },
    provider: 'selfhost',
  },
});

Now, this code above works, contrary to the example in the documentation I don't really know if this is the right way to go about it, could somebody please advise?

Backtracking, I think that part of the docs should be updated, to avoid potential confusion - or maybe I'm in the wrong, I'm not sure, again I ask for a sanity check 🫠

I don't think this needs a reproduction, because the @nuxt/image npm package doesn't seem to contain defineProvider anywhere? https://www.npmjs.com/package/@nuxt/image?activeTab=code

Thanks for your help, Oskar

NetMan134 avatar Apr 18 '25 20:04 NetMan134

Hey, thank you so much for your solution, had the same problem, they should definitely update their docs !

mateocarciu avatar Apr 19 '25 21:04 mateocarciu

This appears to be fixed in the upcoming v2.0.0 release, where the missing export is included. In the meanwhile, you can opt in to the nightly release version by following the docs (be mindful of any breaking changes - https://github.com/nuxt/image/pull/1812).

DamianGlowala avatar Apr 23 '25 11:04 DamianGlowala

Thanks for the solution, I've been going nuts for the past half-hour over this one.

Patrity avatar Jun 12 '25 14:06 Patrity

Thanks for the solution, i have been looking for 15 minutes now.

Niek-Veenstra avatar Jul 12 '25 11:07 Niek-Veenstra

Thank you, this was exactly what I was looking for!

maxmhoff avatar Sep 10 '25 08:09 maxmhoff

Thanks 👍👍

guliyevravan avatar Sep 10 '25 13:09 guliyevravan