Databases and Storage

Image Providers

Resize images on the server or through an image service.

An image provider makes resized copies of each image, called variants, and fills srcset. You set it in media.images:

tenon.config.ts
import { defineCms } from "tenoncms";
import { ipx } from "tenoncms/media/ipx";
import { local } from "tenoncms/media/local";

export default defineCms({
  // ...
  media: {
    storage: local({ dir: ".uploads" }),
    images: { provider: ipx() },
  },
});

Each provider is its own import, tenoncms/media/<name>, so a site loads only the one it uses. npm create tenoncms asks for one and writes its config.

Do you need one?

Without a provider, images are served as uploaded. A read returns the original as src and an empty srcset. That is enough when:

The admin still shows small previews. On upload, the browser draws each image at 320 px on the short side and stores the copy. This needs no package and no work on the server.

Use a provider when you want responsive images without another tool, for example because editors upload large photos.

Your site runs onUse
A Node serveripx()
Cloudflare Workerscloudflare({ binding: env.IMAGES })
Vercelvercel()
Netlifynetlify()
Any host behind Cloudflare with image transformationscloudflare()
Any host, with an image service you already useimgix(), cloudinary(), imagekit(), bunny()
Any host, with your own image serverimgproxy(), thumbor()

Providers

There are two kinds of provider:

  • A rendering provider makes the variants itself. Tenon serves them at /_tenon/media/img/<modifiers>/<key> and stores them in your media storage, under .variants/<key>/. It renders a variant on its first request, or right after the upload with prerender: true. It serves only the variants your config can produce, so nobody can ask for other sizes.
  • A URL provider builds URLs for an image service. Tenon renders and stores nothing. The service MUST be able to load the original.
ProviderImportKindRuns onIgnores
ipx()tenoncms/media/ipxRendering, with sharpNode, Bun, Vercel functionsNothing
cloudflare()tenoncms/media/cloudflareRendering with binding, else URLWorkers with binding, else any site on a Cloudflare zoneNothing
vercel()tenoncms/media/vercelURLVercelheight, fit, format, background, blur
netlify()tenoncms/media/netlifyURLNetlify and netlify devbackground, blur
imgix()tenoncms/media/imgixURLAnywhereNothing
cloudinary()tenoncms/media/cloudinaryURLAnywhereNothing
imagekit()tenoncms/media/imagekitURLAnywhereNothing
bunny()tenoncms/media/bunnyURLAnywherefit, format, background
imgproxy()tenoncms/media/imgproxyURL, self-hostedAnywhereNothing
thumbor()tenoncms/media/thumborURL, self-hostedAnywhereNothing

"Ignores" lists the image options a provider cannot apply.

ipx() renders variants with IPX, which uses sharp. Tenon does not install it, because sharp is a large native package:

npm install ipx@4.0.0-beta.1
tenon.config.ts
import { ipx } from "tenoncms/media/ipx";

images: { provider: ipx({ prerender: true }) },
Renders every variant right after the upload, instead of on its first request. Default: false.
  • sharp is native code. Install the packages on a machine with the same OS and CPU as the server, or in the same container image.
  • tenoncms/media/ipx throws on Cloudflare Workers.

With a prefix on the storage, a URL provider still builds <baseURL>/<key>. Point the service at the prefix folder of the bucket.

Sizes and presets

media.images sets how Tenon builds src and srcset for every image:

tenon.config.ts
import { defineCms } from "tenoncms";
import { ipx } from "tenoncms/media/ipx";
import { local } from "tenoncms/media/local";

export default defineCms({
  // ...
  media: {
    storage: local({ dir: ".uploads" }),
    images: {
      provider: ipx(),
      sizes: [320, 640, 1024, 1536, 1920],
      format: "webp",
      quality: 85,
      presets: {
        avatar: { width: 128, height: 128, fit: "cover" },
      },
    },
  },
});
  • sizes are the widths in srcset. Widths above the original are left out. Default: [320, 640, 1024, 1536, 1920].
  • format and quality apply to every variant. Default: "webp" and 85.
  • presets are named sets of options: width, height, fit, format, quality, background and blur. A field uses one with image: { preset: "avatar" }. A field that names a missing preset stops the server at startup.
  • maxSize is the largest image that gets variants. Default: 50 MB.

SVG files and images above maxSize get the original as src and an empty srcset.

A field can override these options. See Image sizes. Every option is in the configuration reference.

After a change

Stored variants do not follow a change to sizes, presets, format, quality or the provider. Run tenon media regenerate:

npx tenon media regenerate
  • It deletes the stored variants that your options no longer produce.
  • With prerender, it renders the missing ones. --force renders all of them again, for example after a sharp upgrade.
  • A config that runs only on Cloudflare cannot run in the CLI. Call cms.media.regenerate() there, from a route or a job.
  • A URL provider stores nothing. Its new URLs take effect on the next read.

Write your own provider

A provider is an object with getImage(). This one builds URLs for an image service:

cms/my-images.ts
import type { ImageProvider } from "tenoncms";

export const myImages = (baseURL: string): ImageProvider => ({
  getImage({ key, width }, modifiers) {
    const params = new URLSearchParams({ w: String(modifiers.width ?? width) });
    if (modifiers.quality) params.set("q", String(modifiers.quality));
    return { url: `${baseURL}/${encodeURI(key)}?${params}` };
  },
});
Builds the URL of one variant. src holds the storage key, the url of the original, width and height. It runs on the server, on every read.Renders a variant from the original in storage. With it, Tenon serves and stores the variants, like ipx().Renders every variant right after the upload. Needs render.

The URL providers port the mappings of Nuxt Image. Another Nuxt Image provider translates the same way.

  • Media: the media library and responsive images.
  • Image: image fields, their options and what a read returns.
  • Media storage: where the originals and the variants live.
  • tenon media: render the variants again.