Skip to content
Zever
Home

CDN

Purge cached content at the edge with the noop or Cloudflare adapter.

2 min readEdit on GitHub

cdn.CDN invalidates cached content at a CDN provider’s edge so visitors see fresh files immediately. The noop adapter discards purges (the zero-infra default); the cloudflare adapter calls the Cloudflare cache-purge API.

Pick noop for local development (default, no infra); pick cloudflare when files are cached at Cloudflare’s edge.

Terminal window
CDN_ADAPTER=noop

Env vars: CDN_ADAPTER, CDN_API_TOKEN, CDN_ZONE_ID. The cloudflare adapter requires api_token and zone_id at construction; base_url defaults to https://api.cloudflare.com/client/v4. API tokens are never logged: display goes through config.RedactedServices. See the cdn options for every key.

Register the adapter, then resolve from the container, which resolves lazily, caches, and retries after an error; container.New(nil) is valid.

main.go
package main
import (
"context"
cdncloudflare "github.com/zenta-dev/zever/adapters/cdn/cloudflare"
"github.com/zenta-dev/zever/config"
"github.com/zenta-dev/zever/container"
"github.com/zenta-dev/zever/core/cdn"
)
func main() {
cdncloudflare.Register()
ctx := context.Background()
c := container.New(config.Default())
defer c.Close(ctx)
cd, err := c.CDN()
if err != nil {
panic(err)
}
if err := cd.Purge(ctx, cdn.PurgeRequest{
URLs: []string{"https://example.com/assets/app.js"},
}); err != nil {
panic(err)
}
if err := cd.Purge(ctx, cdn.PurgeRequest{
Tags: []string{"static", "v2"},
}); err != nil {
panic(err)
}
if err := cd.Purge(ctx, cdn.PurgeRequest{All: true}); err != nil {
panic(err)
}
}

A zero PurgeRequest is a no-op and returns an error. All: true purges the entire zone. The interface also has Close(ctx) and Name().

Option Type Default Description
api_token string "" Cloudflare API token. Required by the cloudflare adapter. Never logged
zone_id string "" Cloudflare zone identifier. Required by the cloudflare adapter
base_url string https://api.cloudflare.com/client/v4 Cloudflare API endpoint override

The cloudflare adapter has no retry logic: provider errors are returned to the caller. See the cdn options for every key.

Adapter Package Notes
noop adapters/cdn/noop Default, discards purges, closes instantly
cloudflare adapters/cdn/cloudflare Calls the Cloudflare cache-purge API via cloudflare-go/v7; no retry logic

Both serve one interface, so switching is config-only. See the adapters matrix.

Purge returns cdn: empty purge request

Cause: a zero PurgeRequest with no URLs, tags, or All. Fix: set at least one field.

cloudflare: option "api_token" is required

Cause: the cloudflare adapter was selected without credentials. Fix: set api_token and zone_id in config or env (CDN_API_TOKEN, CDN_ZONE_ID).

Purge succeeds but visitors still see stale content

Cause: the URL or tag does not match what the edge cached. Fix: purge by exact URL or tag, or use All: true for the whole zone.