Content Modeling

Media

Uploads, image and file fields, responsive images.

Every Tenon site has a media library. Editors upload images and files into folders, and image and file fields point to them. With an image provider, Tenon also makes resized copies for responsive images.

The media library

Without any config, uploads go to ./.uploads in your project, and images are served as uploaded. The admin shows the library under Media.

Pass a media object to set limits or another storage. The object needs a storage:

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

export default defineCms({
  // ...
  media: {
    storage: local({ dir: "/var/lib/my-site/uploads" }),
    maxSize: 20 * 1024 ** 2,
    accept: ["image/*", "application/pdf"],
  },
});
OptionDefaultWhat it does
storagerequiredWhere files live: local(), s3(), r2() or your own
maxSize100 MBThe largest file it accepts
acceptall typesThe allowed MIME types. image/* works.
imagesfalseResized images. See Responsive images.
accesseveryone readsWho can read, upload, change and delete files
hooksnoneFunctions that run on upload, change, move and delete

media: false turns the library off. image and file fields then stop the server at startup.

Serverless hosts like Vercel and Cloudflare Workers have no disk that keeps files. There, the default storage does not work, and you need a bucket. See Media Storage.

Image and file fields

An image field holds an image from the library. A file field holds any file:

hero: { type: "image", required: true },
attachments: { type: "file", many: true, accept: ["application/pdf"] },

Their options and what a read returns are in Image and File.

Responsive images

Without an image provider, src is the original and srcset is empty. A provider makes resized WebP copies and fills srcset. Tenon supports sharp through ipx, Cloudflare, Vercel, Netlify, imgix and others. See Image providers.

tenon.config.ts
import { resolve } from "node:path";
import { local } from "tenoncms/media/local";
import { ipx } from "tenoncms/media/ipx";

export default defineCms({
  // ...
  media: {
    storage: local({ dir: resolve(".uploads") }),
    images: {
      provider: ipx(),
      sizes: [320, 640, 1024, 1536, 1920],
      presets: {
        avatar: { width: 128, height: 128, fit: "cover" },
      },
    },
  },
});
  • sizes are the widths in srcset. Widths above the original are left out.
  • presets are named sets of options. A field uses one with image: { preset: "avatar" }.
  • A field can also set its own options: image: { sizes: [800, 1600], quality: 70 }. See Image sizes.

These image presets are not the same as the content presets from tenoncms/presets.

Render an image with the Image component. It writes src, srcset, width, height and alt:

---
import { Image } from "tenoncms/astro/components";
---

<Image image={post.hero} sizes="(min-width: 720px) 720px, 100vw" />

Picture shows another image when a media query matches, like a tall crop on phones. See Components.

After you change sizes, presets or the provider, run npx tenon media regenerate to update the stored copies. See the media CLI.

Upload from code

cms.media.upload() adds a file from your own code, for imports and seed scripts:

import { readFile } from "node:fs/promises";
import cms from "./tenon.config";

const image = await cms.media.upload(await readFile("./seed/hero.jpg"), {
  folder: "blog",
  filename: "hero.jpg",
  mimeType: "image/jpeg",
});
await cms.media.update(image.id, { alt: "Harbour at dusk" });

await cms.create("posts", { title: "Hello", hero: image.id });
  • A taken file name gets a counter, like hero-1.jpg.
  • cms.media.update() changes alt, or moves the file with folder and filename.
  • cms.media.delete(id) deletes the file and its copies.

The browser client has the same methods as client.media. See Seed content.