| 1 | import type { MiddlewareHandler } from "astro"; |
| 2 | |
| 3 | /** |
| 4 | * Astro middleware: runs inside the Worker (live) / workerd (dev) for every |
| 5 | * on-demand route. Stamps a fresh per-request id into `Astro.locals` |
| 6 | * (rendered by `/locals`, proving middleware-set locals reach the page) and |
| 7 | * mirrors it onto the outgoing response headers alongside a static marker. |
| 8 | */ |
| 9 | export const onRequest: MiddlewareHandler = async (context, next) => { |
| 10 | const requestId = crypto.randomUUID(); |
| 11 | (context.locals as { requestId?: string }).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 | }; |