Skip to Content
Vuesho

Images

Image sizing, placeholders and optimization.

Images load lazily, and Vuesho reads their width and height when the site builds, so the layout doesn't shift while they load. On a host with an image optimizer, the browser gets each image at the width the screen needs.

Image Paths

An image path takes one of three forms:

content/guide.md
<!-- A file next to the page, bundled with it -->

![The editor](./editor.png)

<!-- A file in `public/`, with the base path added -->

![The logo](/logo.png)

<!-- A remote image -->

![A photo](https://images.example.com/photo.jpg)

A page of the docs

The same paths work in an <img> tag and in <ImageZoom>.

Image Component

In a custom page or a component, Image from vuesho/client renders an image with the same lazy loading, placeholders and optimization:

src/pages/index.vue
<script setup lang="ts">
import { Image, addBasePath } from "vuesho/client";
</script>

<template>
  <Image :src="addBasePath('/hero.png')" :width="1200" :height="630" alt="The editor" preload />
</template>

To replace the component that renders every image, including Markdown images and ImageZoom, fill the image slot.

Image Sizes

The build reads the width and height of each local and remote image, except SVG images. Configure this with markdown.imageSize, or turn it off with imageSize: false:

vuesho.config.ts
createSite(docs.toSource()).config({
  markdown: {
    // Skip remote images, and leave an image without a size when its file can't be read
    imageSize: { external: false, onError: "ignore" },
  },
});

Prop

Type

Missing Images

The build fails at the line of an image whose file doesn't exist, or whose request fails. vuesho dev shows the error when you open the page.

Placeholders

With imagePlaceholder: "blur", a blurred copy of each image shows until the image loads. The build creates the copies with sharp:

npm i -D sharp
vuesho.config.ts
createSite(docs.toSource()).config({
  markdown: { imagePlaceholder: "blur" },
});

Placeholders work for JPEG, PNG, WebP and AVIF files next to the page or in public/, unless they're animated. Remote images, and images in a dynamic source, have none.

Optimization

An image optimizer resizes each image to the widths that screens need, and serves it in a format like WebP. vuesho dev serves the original files, while a deployed site uses its host's optimizer:

The recommended preset includes vercelImage(), which turns on Vercel Image Optimization when the site deploys to Vercel.

Optimizers skip SVG images, data: URLs and an Image with unoptimized. With dangerouslyAllowSVG: true, Vercel and Cloudflare optimize SVG images too.

Optimizer Options

To configure Vercel's or Netlify's optimizer, set image: false on the recommended preset, and add the optimizer yourself:

vuesho.config.ts
import { createSite, recommendedPreset, vercelImage } from "vuesho/config"; 
import { defineDocs } from "vuesho/content";

const docs = defineDocs();

export default createSite(docs.toSource()).plugins(
  recommendedPreset({ image: false }), 
  vercelImage({ formats: ["image/avif", "image/webp"], minimumCacheTTL: 3600 }), 
);

Prop

Type

Remote Images

An optimizer fetches only the remote images that its remotePatterns match. A page with a remote image that no pattern matches fails the build:

vuesho.config.ts
createSite(docs.toSource()).plugins(
  recommendedPreset({ image: false }),
  vercelImage({
    remotePatterns: [{ protocol: "https", hostname: "images.example.com" }], 
  }),
);

A pattern matches each part of a URL, and a part it leaves out matches anything. With selfHostedImage(), a redirect's target must match too.

Prop

Type

Pages Rendered on Request

The build doesn't check the pages of a dynamic source. List their image hosts in remotePatterns, or those images won't load on the deployed site.

Other Optimizers

A plugin can add another image service. Its rootProps hook returns image with a URL template. Vuesho fills the template's {src}, {width} and {quality} for each image:

acme-image.ts
import type { AppShape, Plugin } from "vuesho/config";

export function acmeImage<C extends AppShape>(): Plugin<C> {
  return {
    name: "acme-image",
    rootProps: () => ({
      image: {
        url: "https://images.acme.dev/{src}?w={width}&q={quality}",
        deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
      },
    }),
  };
}

Prop

Type

When the optimizer has remotePatterns, the build checks the remote images against them.