Naijacloud
Docs

Nigerian Edge & image optimisation

The Nigerian Edge keeps copies of a public bucket's files on an edge next to its region, so they're served from close by instead of from storage on every request. Image optimisation resizes images there too, so phones download a small WebP instead of the original.

4 min read

What it does

When the Nigerian Edge is on, your bucket gets a second address:

Code
https://<bucket>.af.cdn.naijacloud.app/<file> # af-west buckets, served from Abuja https://<bucket>.eu.cdn.naijacloud.app/<file> # eu-west buckets, served from Nuremberg

The first request for a file fetches it from storage and keeps a copy on the edge. Every request after that is answered from the copy.

Each region has its own edge, next to its storage. For readers in Nigeria, keep the files in an af-west bucket: that bucket's edge is in Abuja. An eu-west bucket's edge is in Nuremberg, for readers in Europe.

The bucket's own address keeps working exactly as before. Nothing changes until you use the edge address in your app.

Turning it on

Open the bucket, then Properties → Nigerian Edge, and switch on Serve through the Nigerian Edge. Owners and admins can change it.

It needs:

  • A public bucket. The edge serves files to anyone with the link, so a private bucket, or one with Block public access on, can't use it.
  • A bucket in a project. The edge is a project feature. Move the bucket into a project from its Properties.
  • The feature on the project. It is part of the Enterprise plan or the Nigerian Edge add-on. If the card says so, contact us.
  • A bucket name without dots.

When files change

Each copy is checked against storage every minute. If you overwrite a file under the same name, the new version is served within about a minute, even if the file tells browsers to cache it for a year. If storage can't be reached, the edge keeps serving the copy it has.

To drop every copy at once, use Clear cache on the card. The next request for each file fetches it from storage again, so pages may load a little slower for a few minutes. Nothing in the bucket changes.

Your files' own Cache-Control, Content-Type and other metadata reach the browser unchanged. Files of 100 MB or more, and requests for part of a file (video seeking, resumed downloads), stream straight from storage without a copy.

Image optimisation

With Image optimisation on, add /_img/w=<width>/ before a file's name to get it resized:

Code
https://photos.af.cdn.naijacloud.app/_img/w=640/shop/shoe.jpg # WebP, 640 px wide https://photos.af.cdn.naijacloud.app/_img/w=256,f=jpeg/shop/shoe.jpg # JPEG
  • Widths: 16, 32, 48, 64, 96, 128, 256, 384, 640, 750, 828, 1080, 1200, 1920, 2048 or 3840. Any other width is refused.
  • Formats: WebP by default; add ,f=jpeg or ,f=png after the width.
  • Each size is made once and then served from the edge like any other file.
  • If an image can't be resized (it isn't an image, or the edge is busy), the original is served, so the page still shows it.

Monthly cap

Each bucket has a monthly cap on resizes, 50,000 unless you change it under Monthly resize cap. Past the cap, original images are served until the month ends (UTC), so your site keeps working and the count stops. The card shows how many resizes you've used.

With Next.js

Point next/image at the edge with a custom loader. Next.js asks for the same widths the edge makes.

image-loader.js
js
export default function edgeLoader({ src, width }) { return `https://photos.af.cdn.naijacloud.app/_img/w=${width}/${src.replace(/^\//, '')}`; }
next.config.js
js
module.exports = { images: { loader: 'custom', loaderFile: './image-loader.js' }, };

Custom domains

A bucket with a custom domain is served through the edge on that domain too, while the edge is on. Nothing changes in your DNS.

Usage

The card shows this month's traffic through the edge, the share answered from the copy on the edge, and the resizes used. Both are counted per bucket, per hour.