Media
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:
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"],
},
});
| Option | Default | What it does |
|---|---|---|
storage | required | Where files live: local(), s3(), r2() or your own |
maxSize | 100 MB | The largest file it accepts |
accept | all types | The allowed MIME types. image/* works. |
images | false | Resized images. See Responsive images. |
access | everyone reads | Who can read, upload, change and delete files |
hooks | none | Functions that run on upload, change, move and delete |
media: false turns the library off. image and file fields then stop the server at startup.
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.
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" },
},
},
},
});
sizesare the widths insrcset. Widths above the original are left out.presetsare named sets of options. A field uses one withimage: { 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" />
<template>
<TenonImage :image="post.hero" sizes="(min-width: 720px) 720px, 100vw" />
</template>
import { Image } from "tenoncms/react";
<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()changesalt, or moves the file withfolderandfilename.cms.media.delete(id)deletes the file and its copies.
The browser client has the same methods as client.media. See Seed content.