Request Pipeline

Astro 7 stabilizes advanced routing. The default pipeline still works for normal projects, but you can now replace or compose it from src/fetch.ts.

Default Pipeline

Astro’s built-in request flow handles common SSR concerns in a fixed order:

flowchart LR
    req[Request] --> slash["Trailing slash"]
    slash --> redirects[Redirects]
    redirects --> sessions[Sessions]
    sessions --> actions[Actions]
    actions --> middleware[Middleware]
    middleware --> pages[Pages and endpoints]
    pages --> i18n[i18n]
    i18n --> cache[Cache]
    cache --> res[Response]

For most apps, keep this pipeline.

Advanced Routing

Add src/fetch.ts when the default pipeline order is wrong for your app.

import { FetchState, astro } from 'astro/fetch';

export default {
  async fetch(request: Request): Promise<Response> {
    const state = new FetchState(request);
    const url = new URL(request.url);

    if (url.pathname.startsWith('/admin')) {
      const cookie = request.headers.get('cookie') ?? '';
      if (!cookie.includes('session=')) {
        return Response.redirect(new URL('/login', request.url));
      }
    }

    return astro(state);
  },
};

Rename Or Disable The Entrypoint

src/fetch.ts is now reserved. If you already used that filename for another purpose, configure fetchFile:

import { defineConfig } from 'astro/config';

export default defineConfig({
  fetchFile: 'handler',
});

Or disable advanced routing entrypoint detection:

import { defineConfig } from 'astro/config';

export default defineConfig({
  fetchFile: null,
});

Hono Composition

Astro 7 exposes Hono-compatible wrappers:

import { Hono } from 'hono';
import { logger } from 'hono/logger';
import { actions, middleware, pages, i18n } from 'astro/hono';

const app = new Hono();

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

export default app;

When To Use Advanced Routing

Use it for:

  • auth before Astro Actions
  • custom API delegation before pages
  • response timing around a subset of the pipeline
  • Hono middleware you want to own explicitly

Do not use it just to add normal page middleware. Astro middleware still covers most needs.