Cloudflare with Astro

Most of an Astro site is files, and files are what a CDN is for. Cloudflare serves them from wherever the visitor is, and the interesting part of the setup is the small list of things that are not files: the redirects a migration leaves behind, the headers, and any route that has to do work when it is asked rather than when it is built.

Cloudflare integrations like this usually take 2–4 days

How the handoff works.

  1. 01

    Build

    Pages rendered before anyone asks

  2. 02

    Rules

    Redirects and headers generated with them

    Key handoff

  3. 03

    Upload

    Assets pushed to the edge

  4. 04

    Serve

    Delivered from near the visitor

  5. 05

    Verify

    Every redirect requested, not sampled

Why this is the key handoff

Built once, served everywhere. The build produces files and a redirect list. Cloudflare serves the first from the edge and applies the second before anything else is looked up, which is why a migration's redirects belong in the host rather than in the site.

Three things that commonly go wrong.

  1. 01

    Redirect order is not alphabetical, and it matters

    A wildcard placed above a specific rule makes that rule unreachable, and nothing warns you: the specific address quietly lands wherever the wildcard points. After a migration this is the failure that sends the six most valuable old URLs to the home page. The list is generated in a deliberate order and then requested, not read.

  2. 02

    Not everything an Astro site can do survives on a static host

    Anything wanting a server at request time needs a Worker, and Workers do not offer the whole Node API. A route that works locally can fail on deploy for the want of one module, so which routes need a request is a decision made before the build, not discovered after it.

  3. 03

    Scheduled work is the thing people forget

    A static site publishes what it was built with, so a post dated tomorrow appears when something rebuilds. On a host without a scheduler, that something is a person. Worth deciding up front whether a daily rebuild is automated or somebody's job.

What is included.

  • The built site served as static assets from the edge

  • Redirects from the migration, in the host's own format

  • Security and caching headers set per path

  • Routes that must run per request, where any exist

  • A custom domain, with the apex and www agreeing

Reach out and see if we are a good fit.

Get in touch

Currently booking two to four weeks out.