Guide

Use Fileboy as an image CDN for next/image

A custom loader that asks Fileboy for the width and quality Next.js wants, with no image processing in your app.

Images guide · in this guide

  • What a custom loader does
  • 1. The loader
  • 2. Turn it on
  • 3. Use it
  • Limits to know

Published

What a custom loader does

next/image works out which widths a page needs and asks a loader for the URL of each. By default the loader points at Next.js's own image optimiser, which runs on your server or your host. A custom loader points at another service instead. With Fileboy, each width is a URL that resizes the stored original, so your Next.js app does no image processing.

The src of each <Image> is then a Fileboy file ID, the one you stored when the file was uploaded.

1. The loader

A loader is a function from { src, width, quality } to a URL. This one has no dependencies:

fileboyLoader.ts
import type { ImageLoaderProps } from "next/image"

export default function fileboyLoader({ src, width, quality }: ImageLoaderProps) {
  // Every <Image> in the app comes here: leave full and data: URLs as they are
  if (/^(\/\/|(https?|data|blob):)/i.test(src)) return src
  // Local files (/public, static imports) are served by the app; the width stops Next.js warning
  if (src.startsWith("/")) return `${src}${src.includes("?") ? "&" : "?"}w=${width}`
  const transform = `resize:${width}x_~upscale:false;format:auto;quality:${quality ?? 75}`
  return `https://api.fileboy.io/images/get/${src}?transform=${transform}`
}

In the transform, resize:640x_ sets the width and keeps the shape, upscale:false never makes an image bigger than its original, format:auto picks the smallest format the browser accepts, and quality is the quality prop of the <Image>, 75 unless you set one. The transform reference lists everything else you could add.

The @cd2/fileboy-next package has the same function as @cd2/fileboy-next/loader, with a check on its inputs and options for the base URL, the default quality and the format. It is in the package's source and README now and ships in the release after 0.1.0.

2. Turn it on

next.config.ts
import type { NextConfig } from "next"

const config: NextConfig = {
  images: {
    loader: "custom",
    loaderFile: "./fileboyLoader.ts",
  },
}

export default config

Every <Image> in the app now uses the loader. You do not need remotePatterns, because Next.js does not fetch the images itself. A src that is a path (a file in public, or a static import) or a full URL is passed through as it is, so those images still work, unresized; give the loader a Fileboy file ID to get the resizing.

3. Use it

app/products/[id]/page.tsx
import Image from "next/image"

export default async function Product() {
  const product = await getProduct() // { photoId: "..." }, the ID you stored at upload
  return (
    <Image
      src={product.photoId}
      alt="The product"
      width={1200}
      height={800}
      sizes="(min-width: 768px) 50vw, 100vw"
      priority
    />
  )
}

width and height stop the page jumping, and sizes lets the browser pick the right entry from the srcset that Next.js builds. Without sizes, Next.js only offers 1x and 2x versions of the stated width. For an image that fills its parent, use fill with sizes instead of a width and height.

Limits to know

  • The loader doesn't sign URLs. It is for apps that serve images without signed URLs. For private images, sign on your server and pass the signed URL to a plain <img>.
  • It is for images. Other files, such as video, have their own player.
  • To crop or add a watermark, add steps to the transform string in the loader: the ; joins steps. See crop avatars.
  • To upload from the same app, see uploading in the App Router.

Checked against the current libraries in November 2026. Found something that no longer works? Tell us.

Still choosing a tool?

Fileboy is small on purpose. This comparison says where it fits and where another option is the better call.

Get a key and run this code

Create a free app, copy your keys into the example and run it. The free plan needs no card.