1import type { MiddlewareHandler } from "astro";
2
3/**
4 * Astro middleware: runs inside workerd for every on-demand route (dev +
5 * live). Stamps a fresh per-request id into `Astro.locals` (rendered by the
6 * SSR pages, proving each request renders anew) and mirrors it onto the
7 * outgoing response headers.
8 */
9export const onRequest: MiddlewareHandler = async (context, next) => {
10 const requestId = crypto.randomUUID();
11 context.locals.requestId = requestId;
12 const response = await next();
13 response.headers.set("x-request-id", requestId);
14 response.headers.set("x-middleware", "hit");
15 return response;
16};