Vercel adapter stamps 404 on paths that Astro middleware rewrites to valid routes (server output)
Summary
With output: 'server' on the Vercel adapter, any path that Astro middleware rewrites to a valid route (e.g. locale-prefixed i18n URLs de-localized to clean paths) is served with the correct HTML but a 404 HTTP status, because the platform routing layer forces a 404 before the middleware/function runs.
Repro
// astro.config.mjs
import vercel from '@astrojs/vercel';
import { defineConfig } from 'astro/config';
export default defineConfig({
output: 'server',
adapter: vercel(),
});// src/middleware.ts — de-localizes locale-prefixed URLs (paraglide/similar i18n pattern)
export const onRequest = (context, next) => {
const [, first] = context.url.pathname.split('/');
if (['en', 'fr'].includes(first)) {
const url = new URL(context.url);
url.pathname = url.pathname.replace(new RegExp(`^/${first}`), '') || '/';
return next(url);
}
return next();
};Pages live at clean paths (src/pages/index.astro, src/pages/about.astro, src/pages/404.astro). Requesting / as /en (public URL kept via the middleware rewrite):
- Expected:
GET /en/about→200 - Actual:
GET /en/about→404(body is the correctaboutpage — the HTML renders fine)
Root cause
packages/integrations/vercel/src/index.ts emits a catch-all route for unmatched paths:
finalRoutes.push({
src: '/.*',
dest: fourOhFourRoute.isPrerendered
? '/404.html'
: _middlewareEntryPoint ? MIDDLEWARE_PATH : NODE_PATH,
status: 404,
});The platform evaluates this catch-all for any path not in the static route table (^/, ^/about, ...). Rewrites performed inside Astro middleware are invisible to that table, so /en/about falls into the catch-all and gets status: 404 stamped by Vercel — even though the render function produces a 200 for the rewritten request. The 404 only applies when there is no route; the server-side renderer can already decide that itself (Astro sets a 404 on genuinely unmatched paths via 404.astro).
Suggested fix
Only force status: 404 when the fallback destination is the static /404.html; when the 404 page is server-rendered, forward to the render function without a forced status so the function returns the real status (200 for middleware-rewritten routes, 404 from 404.astro otherwise).
Source: withastro/astro