Skip to content

Add Cloudflare fetch and Hono handlers for advanced routing#16729

Merged
matthewp merged 3 commits into
mainfrom
cloudflare-handlers
May 27, 2026
Merged

Add Cloudflare fetch and Hono handlers for advanced routing#16729
matthewp merged 3 commits into
mainfrom
cloudflare-handlers

Conversation

@matthewp

@matthewp matthewp commented May 13, 2026

Copy link
Copy Markdown
Contributor

@astrojs/cloudflare/fetch

import { astro, FetchState } from 'astro/fetch';
import { cf } from '@astrojs/cloudflare/fetch';

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    const state = new FetchState(request);
    const asset = await cf(state, env, ctx);
    if (asset) return asset;
    return astro(state);
  }
}

@astrojs/cloudflare/hono

import { Hono } from 'hono';
import { actions, middleware, pages, i18n } from 'astro/hono';
import { cf } from '@astrojs/cloudflare/hono';

const app = new Hono<{ Bindings: Env }>();

app.use(cf());
app.use(actions());
app.use(middleware());
app.use(pages());
app.use(i18n());

export default app;

Changes

  • Adds @astrojs/cloudflare/fetch exporting a cf(state, env, ctx) function that applies Cloudflare-specific setup to a FetchState — session KV binding injection, static asset serving via ASSETS, locals.cfContext, client address from cf-connecting-ip, waitUntil, and prerendered error page fetch. Returns a Response for asset hits, undefined otherwise.
  • Adds @astrojs/cloudflare/hono exporting a cf() Hono middleware that does the same setup, reading env and executionCtx from the Hono context automatically (no arguments needed).
  • Extracts shared helpers from the existing handler.ts into cf-helpers.ts and cf.ts, then refactors handler.ts to use them. Zero duplication between the handle() entrypoint and the new exports.

Testing

  • Adds cf-helpers.test.ts with 18 unit tests covering createLocals, getClientAddress, matchStaticAsset, fallbackToAssets, and createErrorPageFetch. Tests use mock Env and ExecutionContext objects — no fixtures or build required.
  • All 207 existing Cloudflare adapter tests continue to pass after the handler.ts refactor.

Docs

@changeset-bot

changeset-bot Bot commented May 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7186049

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 36 packages
Name Type
@astrojs/cloudflare Minor
@test/astro-cloudflare-node-prerender-mdx Patch
@test/astro-cloudflare Patch
@test/astro-cloudflare-allowed-hosts Patch
@test/astro-cloudflare-astro-dev-platform Patch
@test/astro-cloudflare-astro-env Patch
@test/astro-cloudflare-binding-image-service Patch
@test/astro-cloudflare-cache-provider-wait-until Patch
@test/astro-cloudflare-client-address Patch
@test/astro-cloudflare-compile-image-service Patch
@test/astro-cloudflare-custom-entryfile Patch
@test/astro-cloudflare-dev-image-endpoint Patch
@test/astro-cloudflare-external-image-service Patch
@test/astro-cloudflare-external-redirects Patch
@test/astro-cloudflare-internal-redirects Patch
@test/astro-cloudflare-no-output Patch
@test/astro-cloudflare-prerender-node-env Patch
@test/astro-cloudflare-prerender-queue-consumers Patch
@test/astro-cloudflare-prerender-styles Patch
@test/astro-cloudflare-prerenderer-errors Patch
@test/routing-priority-cloudflare Patch
@test/cf-server-entry Patch
@test/astro-cloudflare-server-island-prerender-framework Patch
@test/astro-cloudflare-sessions Patch
@test/astro-cloudflare-sql-import Patch
@test/cf-ssr-deps Patch
@test/astro-cloudflare-static Patch
@test/astro-cloudflare-svelte-rune-deps Patch
@test/astro-cloudflare-top-level-return Patch
@test/astro-cloudflare-vite-plugin Patch
@test/astro-cloudflare-with-base Patch
@test/astro-cloudflare-with-react Patch
@test/astro-cloudflare-with-solid-js Patch
@test/astro-cloudflare-with-svelte Patch
@test/astro-cloudflare-with-vue Patch
@test/astro-cloudflare-wrangler-preview-platform Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the pkg: integration Related to any renderer integration (scope) label May 13, 2026
Comment thread packages/integrations/cloudflare/src/utils/handler.ts
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Package Trust Level Decreased

Caution

Decreased trust levels may indicate a higher risk of supply chain attacks. Please review these changes carefully.

📦 Package 🔒 Before 🔓 After
@clack/core trusted-with-provenance none
@clack/prompts trusted-with-provenance none

@matthewp matthewp marked this pull request as ready for review May 18, 2026 20:35

@florian-lefebvre florian-lefebvre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if this is in experimental, it should be documented I think. Also I don't remember but how does that work with main in the wrangler config?

@ematipico ematipico left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new APIs must be documented: https://docs.astro.build/en/guides/integrations-guide/cloudflare/

Doesn't matter if they are used with an experimental feature.

@matthewp

Copy link
Copy Markdown
Contributor Author

Ok, do we have any other examples of this happening? Want to see what it looks like on the docs site.

@ematipico

Copy link
Copy Markdown
Member

Ok, do we have any other examples of this happening? Want to see what it looks like on the docs site.

Usually everything stays on the same page, also APIs that were added to adapters/integrations as part of the experimental feature.

I did this whe I added adapter static headers for CSP

@matthewp

Copy link
Copy Markdown
Contributor Author

Docs added here: withastro/docs#13912

@Princesseuh Princesseuh added this to the 6.4 milestone May 26, 2026
@matthewp matthewp merged commit 01aa164 into main May 27, 2026
22 checks passed
@matthewp matthewp deleted the cloudflare-handlers branch May 27, 2026 12:27
@astrobot-houston astrobot-houston mentioned this pull request May 26, 2026
matthewp added a commit that referenced this pull request May 30, 2026
)

## Goal
Eliminate the 'new dependencies optimized: astro/fetch' warning during dev
in the Cloudflare adapter. The advanced routing feature (d69f858, #16366)
introduced astro/fetch and astro/hono exports, and the Cloudflare handler
entry points (01aa164, #16729) import from them — but neither commit
added these to the optimizeDeps.include list. Vite discovers them at runtime
and triggers a late re-optimization.

## Decisions
- Added both astro/fetch and astro/hono: the Cloudflare adapter imports from
  both (hono.ts imports FetchState from astro/fetch, and astro/hono is used
  similarly). Pre-bundling both prevents the same issue for either entry point.

## Changes
- packages/integrations/cloudflare/src/index.ts: added astro/fetch and
  astro/hono to the server environment optimizeDeps.include array, alongside
  the existing astro/app and astro/app/fetch/default-handler entries.
ematipico added a commit that referenced this pull request Jun 2, 2026
* chore(deps): resolve peer dependency issues (#16894)

* chore(deps): update eslint to v10 (#16896)

* test(vercel): add per-test timeout and raise suite timeout (#16898)

* chore: eliminate duplicate pnpm configurations (#16906)

* Fix i18n redirectToDefaultLocale not working with prefixDefaultLocale (#16887)

* fix(i18n): preserve ROUTE_TYPE_HEADER for i18n post-processing (#16800)

* chore: move fix somewhere else

* chore: update changeset

---------

Co-authored-by: ematipico <estoppa@cloudflare.com>

* feat(fonts): better bold fallbacks (#16908)

* [ci] format

* fix(docs): replace last occurrences of `withastro/adapters` (#16693)

* fix: astro() marks all pipeline features as used (#16899)

* fix: astro() marks all pipeline features as used

AstroHandler is the batteries-included handler that wires up every
pipeline feature internally. Mark all PipelineFeatures bits at the
top of handle() so the one-shot warnMissingFeatures check in BaseApp
never fires a false positive when the first request short-circuits
(e.g. a redirect route skips middleware).

* add changeset

* move allFeatures mask to base-pipeline.ts per review

* docs: fix typos in astro:i18n middleware JSDoc (#16574)

* docs: fix outdated Partytown link (#16865)

* chore(deps): update devalue (#16900)

* Pre-bundle astro/fetch and astro/hono in Cloudflare optimizeDeps (#16914)

## Goal
Eliminate the 'new dependencies optimized: astro/fetch' warning during dev
in the Cloudflare adapter. The advanced routing feature (d69f858, #16366)
introduced astro/fetch and astro/hono exports, and the Cloudflare handler
entry points (01aa164, #16729) import from them — but neither commit
added these to the optimizeDeps.include list. Vite discovers them at runtime
and triggers a late re-optimization.

## Decisions
- Added both astro/fetch and astro/hono: the Cloudflare adapter imports from
  both (hono.ts imports FetchState from astro/fetch, and astro/hono is used
  similarly). Pre-bundling both prevents the same issue for either entry point.

## Changes
- packages/integrations/cloudflare/src/index.ts: added astro/fetch and
  astro/hono to the server environment optimizeDeps.include array, alongside
  the existing astro/app and astro/app/fetch/default-handler entries.

* fix(node): serve prerendered pages with build.format 'file' and 'preserve' by passing extensions option to send (#16922)

* [ci] format

* chore(deps): update `pnpm/action-setup` to v6 (#16904)

* chore(deps): update pnpm to v11.5.0 (#16903)

* Fix 404 route resolution for experimental.advancedRouting with astro/hono handlers (#16911)

Co-authored-by: Emanuele Stoppa <estoppa@cloudflare.com>
Co-authored-by: Matthew Phillips <matthew@matthewphillips.info>

* dedupe

---------

Co-authored-by: ocavue <ocavue@users.noreply.github.com>
Co-authored-by: btea <2356281422@qq.com>
Co-authored-by: Houston (Bot) <108291165+astrobot-houston@users.noreply.github.com>
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Co-authored-by: Florian Lefebvre <florian-lefebvre@users.noreply.github.com>
Co-authored-by: Armand Philippot <git@armand.philippot.eu>
Co-authored-by: Matthew Phillips <matthewphillips@cloudflare.com>
Co-authored-by: dfedoryshchev <64079946+dfedoryshchev@users.noreply.github.com>
Co-authored-by: Andreas Deininger <andreas@deininger.net>
Co-authored-by: Houston (Bot) <matthewp@users.noreply.github.com>
Co-authored-by: Matthew Phillips <matthew@matthewphillips.info>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants