<!-- Markdown twin of https://pixelvault.dev/blog/astro-image-optimization-pixelvault/ -->

[← Back to blog](/blog/)

By [PixelVault](/about/) November 27, 2026

# Astro image optimization and Content Collections with PixelVault

Astro's built-in Sharp pipeline is great for a handful of local assets, but once your site hosts hundreds of remote screenshots, product photos, or galleries, downloading and re-encoding every variant on every build slows deploys down. Today we're releasing `@pixelvault-dev/astro` to move that work to the edge.

The package works with Astro 5 and Astro 6, ships with zero runtime dependencies, and is open source at [github.com/pixelvault-dev/astro](https://github.com/pixelvault-dev/astro). We already dogfood it on `pixelvault.dev` itself (which is an Astro 6 static site).

```
npm install @pixelvault-dev/astro
```

## 1\. Drop-in `<PixelVaultImage />` component

If you want to keep Astro's default Sharp image service untouched for local assets, you can import `<PixelVaultImage />` directly without adding any integration to `astro.config.mjs`:

```
---
import PixelVaultImage from "@pixelvault-dev/astro/PixelVaultImage.astro";
---

<PixelVaultImage
  src="https://img.pixelvault.dev/proj_xyz789/img_abc123.jpg"
  width={800}
  height={450}
  widths={[400, 800, 1200]}
  sizes="(max-width: 800px) 100vw, 800px"
  alt="Dashboard screenshot"
/>
```

Under the hood, the component handles four details that are easy to get wrong by hand:

-   **Snapped responsive `srcset`:** widths are snapped up and deduplicated against PixelVault's discrete dimension whitelist (`16`–`4000`), so two nearby breakpoints never waste edge cache variants.
-   **Clean proportional resizing:** `width` and `height` stay on the HTML `<img>` tag to prevent layout shift (CLS), while `&h=` is only sent to the CDN when you ask for a box fit (`cover`, `crop`, `contain`, `pad`) or omit `width`.
-   **Full transform props:** named presets (`size="social"` or custom project presets), AI background removal (`segment="foreground"`), face-aware crop (`gravity="face"`), blur, sharpen, rotate, watermarks, and text overlays are all typed props.
-   **Safe passthrough:** HMAC-signed private URLs (`?token=…&expires=…`), SVGs, and PDFs are never mutated, so signed links never break with a `403`.

## 2\. Offload Astro's built-in `<Image />` to the edge

If your pages already use Astro's standard `<Image />`, `<Picture />`, or `getImage()` from `astro:assets`, register the integration in `astro.config.mjs`:

```
// astro.config.mjs
import { defineConfig } from "astro/config";
import pixelvault from "@pixelvault-dev/astro";

export default defineConfig({
  integrations: [
    pixelvault({
      domains: ["cdn.example.com"], // optional custom domains
      defaultFormat: "auto",
    }),
  ],
});
```

That adds `img.pixelvault.dev` (plus any [custom domains](/docs/guides/custom-domains/)) to Astro's allowed remote domains and registers PixelVault's `ExternalImageService`. Standard `<Image />` tags now emit edge-transformed PixelVault URLs directly with zero Sharp processing:

```
---
import { Image } from "astro:assets";
---

<Image
  src="https://img.pixelvault.dev/proj_xyz789/img_abc123.jpg"
  width={800}
  height={450}
  widths={[400, 800, 1600]}
  sizes="(max-width: 800px) 100vw, 800px"
  alt="Hosted on PixelVault"
/>
```

Local ESM-imported images, non-PixelVault remote URLs, signed URLs, and SVGs pass through untouched. If you prefer to keep Sharp for local files and only whitelist your PixelVault domains, pass `imageService: false`.

## 3\. Sync media libraries into Content Collections

Astro's Content Layer API makes it straightforward to pull a PixelVault folder, collection, or AI-tagged subset into a typed collection at build time. The loader reads `PIXELVAULT_API_KEY` from your environment, paginates automatically, and prunes deleted assets between builds:

```
// src/content.config.ts
import { defineCollection } from "astro:content";
import { pixelvaultImagesLoader } from "@pixelvault-dev/astro/loader";

const gallery = defineCollection({
  loader: pixelvaultImagesLoader({
    folder: "portfolio",
    kind: "image",
  }),
});

export const collections = { gallery };
```

Because PixelVault's [AI descriptions](/docs/guides/ai-descriptions/) live on each image object, you can render a full responsive gallery with alt text straight from the collection:

```
---
import { getCollection } from "astro:content";
import PixelVaultImage from "@pixelvault-dev/astro/PixelVaultImage.astro";

const photos = await getCollection("gallery");
---

{photos.map(({ data }) => (
  <PixelVaultImage
    src={data.branded_url ?? data.url}
    width={data.width ?? 800}
    height={data.height ?? 600}
    widths={[400, 800, 1200]}
    sizes="(max-width: 768px) 100vw, 33vw"
    alt={data.description?.alt ?? data.filename ?? ""}
  />
))}
```

Need video or audio collections instead? Use `pixelvaultVideosLoader()` or `pixelvaultAudioLoader()` from `@pixelvault-dev/astro/loader`.

## Get started

Read the full [Astro integration docs](/docs/astro/), check the [image transform reference](/docs/transforms/), or browse the source and file issues on [GitHub](https://github.com/pixelvault-dev/astro).

## More in Host images for your stack

-   [How to host an MP3 or audio file for a direct URL](/blog/host-mp3-audio-direct-url/) Why Google Drive and Dropbox audio links break in HTML5 players and APIs, and how to get a direct, seekable MP3/WAV/M4A/OGG CDN URL in one step.
-   [How marketing teams can build a media pipeline for social content](/blog/media-pipeline-for-social-content/) Collect or generate campaign images, video and audio, host them once on PixelVault for stable URLs, then schedule and publish them with Social Champ.
-   [HEIC won't open or upload? Turn an iPhone photo into a link](/blog/heic-to-jpg-link-iphone-photos/) iPhones save photos as HEIC, and most of the web can't display them. Why it happens, the quickest way to a working link, and how to stop it at the source.

[All posts in Host images for your stack →](/blog/topics/integrations/)
