Astro throws “The middleware returned something that is not a Response object” when onRequest returns a value other than a Response, such as a string, a plain object or the next function itself. Middleware must return the result of next(), a new Response(...), or a helper that builds one, like context.redirect() or context.rewrite(). Wrap data in Response.json() if you meant to send JSON.
Key Takeaways
- Valid returns:
next(),await next(),new Response(),Response.json(),context.redirect(),context.rewrite().- Returning a string, object, number or
next(without calling it) triggers the error.- If you call
next(), return its result or return nothing; do not return other data.- Share data with pages through
context.locals, not the return value.- Returning nothing without calling
next()is a different error.
The error
The middleware returned something that is not a `Response` object.
Any data returned from middleware must be a valid `Response` object.
Why it happens
Astro checks the value your middleware returns. Anything that is not a Response instance fails. Typical mistakes:
// src/middleware.ts (each of these fails)
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware(async (context, next) => {
if (context.url.pathname === '/api/ping') {
return { ok: true }; // plain object
}
if (context.url.pathname === '/old') {
return '/new'; // string
}
await next();
return context.locals.user; // data instead of the response
});
Returning next instead of next() fails the same way, because it returns a function.
Fix: return a Response
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware(async (context, next) => {
if (context.url.pathname === '/api/ping') {
return Response.json({ ok: true });
}
if (context.url.pathname === '/old/') {
return context.redirect('/new/', 301);
}
return next();
});
To modify the page response, await next(), change it, and return it:
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware(async (context, next) => {
const response = await next();
const html = await response.text();
return new Response(html.replace('%YEAR%', String(new Date().getFullYear())), {
status: response.status,
headers: response.headers,
});
});
Fix: pass data through locals
If you returned data so a page could read it, put it on context.locals before calling next():
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware((context, next) => {
context.locals.requestId = crypto.randomUUID();
return next();
});
Pages read it as Astro.locals.requestId. Declare its type in src/env.d.ts under App.Locals; our Astro locals guide shows how, and locals reassigned covers a related mistake.
If your middleware returns nothing and never calls next(), Astro reports a separate error; see middleware returned no response. The middleware guide explains the full flow, the Astro config docs cover project setup, and the official middleware reference lists every helper. Static themes from the Astro themes catalog work with middleware once you add it, but it only runs at build time unless pages render on demand.
FAQ
What should Astro middleware return?
A Response: either the one from next(), or one you create with new Response(), Response.json(), context.redirect() or context.rewrite().
How do I return JSON from Astro middleware?
Return Response.json(data). For more control over headers and status, use new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } }).
Do I have to return next() in Astro middleware?
You should return its result. If you call next() and return nothing, Astro uses the response from next(), but returning it explicitly is clearer and avoids mistakes.