Skip to content

Commit ea8c27b

Browse files
matthewpArmandPhilippotyanthomasdevflorian-lefebvreematipico
authored
Add Cloudflare adapter section to advanced routing docs (#13912)
Co-authored-by: Armand Philippot <git@armand.philippot.eu> Co-authored-by: Yan <61414485+yanthomasdev@users.noreply.github.com> Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com> Co-authored-by: ematipico <602478+ematipico@users.noreply.github.com>
1 parent e5b18a3 commit ea8c27b

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

src/content/docs/en/reference/experimental-flags/advanced-routing.mdx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,53 @@ export default app;
477477
```
478478

479479
The `astro/hono` module exports the same handler names as `astro/fetch` (`astro`, `pages`, `middleware`, `actions`, `sessions`, `redirects`, `cache`, `i18n`, `trailingSlash`), but each returns a Hono middleware function. This lets you mix Astro handlers with any Hono middleware from the ecosystem.
480+
481+
## Cloudflare adapter
482+
483+
The [`@astrojs/cloudflare`](/en/guides/integrations-guide/cloudflare/) adapter provides companion handlers that apply Cloudflare-specific setup to your advanced routing pipeline. These handlers configure session KV binding injection, static asset serving via the `ASSETS` binding, `Astro.locals.cfContext`, client address from the `cf-connecting-ip` header, `waitUntil`, and prerendered error page fetching.
484+
485+
You can use the `astro/fetch` and `astro/hono` APIs from `src/app.ts` on Cloudflare without these handlers. The adapter's default entrypoint takes care of Cloudflare-specific setup for you. These companion handlers are useful when you already have a [custom worker entrypoint](https://developers.cloudflare.com/workers/wrangler/configuration/#inheritable-keys) (`src/worker.ts`), for example, to export a Durable Object, and want to use the advanced routing APIs directly from that file instead.
486+
487+
When using these handlers in your worker entrypoint, they replace the functionality of the adapter's default handler, so you should not use both at the same time. Place the Cloudflare handler before other Astro handlers so that bindings and locals are available to the rest of the pipeline.
488+
489+
### `@astrojs/cloudflare/fetch`
490+
491+
<p><Since v="13.6.0" pkg="@astrojs/cloudflare" /></p>
492+
493+
For use with `astro/fetch`. The `cf()` function receives a `FetchState`, the Cloudflare `env`, and the `ExecutionContext`. It returns a `Response` for static asset hits, or `undefined` when the request should continue to Astro rendering:
494+
495+
```ts title="src/worker.ts"
496+
import { astro, FetchState } from 'astro/fetch';
497+
import { cf } from '@astrojs/cloudflare/fetch';
498+
499+
export default {
500+
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
501+
const state = new FetchState(request);
502+
const asset = await cf(state, env, ctx);
503+
if (asset) return asset;
504+
return astro(state);
505+
},
506+
};
507+
```
508+
509+
### `@astrojs/cloudflare/hono`
510+
511+
<p><Since v="13.6.0" pkg="@astrojs/cloudflare" /></p>
512+
513+
For use with `astro/hono`. The `cf()` function returns a Hono middleware that reads `env` and `executionCtx` from the Hono context automatically:
514+
515+
```ts title="src/worker.ts"
516+
import { Hono } from 'hono';
517+
import { actions, middleware, pages, i18n } from 'astro/hono';
518+
import { cf } from '@astrojs/cloudflare/hono';
519+
520+
const app = new Hono<{ Bindings: Env }>();
521+
522+
app.use(cf());
523+
app.use(actions());
524+
app.use(middleware());
525+
app.use(pages());
526+
app.use(i18n());
527+
528+
export default app;
529+
```

0 commit comments

Comments
 (0)