Summary
Remove the deprecated useAPIMiddleware option and the generated #ANALOG_API_MIDDLEWARE proxy middleware from @analogjs/vite-plugin-nitro. API routes should be served exclusively through Nitro's native file-based routing under src/server/routes/api.
Background
@analogjs/vite-plugin-nitro currently supports three ways of handling the /api prefix (packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts):
hasAPIDir === true — a src/server/routes/api directory exists; Nitro serves those routes natively. (Recommended, and the default for every scaffold.)
hasAPIDir === false && useAPIMiddleware === true — the default when no api directory exists. Injects the generated #ANALOG_API_MIDDLEWARE, which matches the /api prefix and forwards the stripped request to $fetch/proxyRequest (fetchWithEvent/proxyRequest on the alpha line).
hasAPIDir === false && useAPIMiddleware === false — falls back to a Nitro routeRules proxy ({ '/api/**': { proxy: { to: '/**' } } }).
useAPIMiddleware is already marked @deprecated in options.ts, with guidance to use the src/server/routes/api folder instead.
Motivation
- Redundancy. Every first-party scaffold (all
create-analog templates and both @analogjs/nx-plugin generators) already ships a populated src/server/routes/api directory, so path 2 is only reached by projects that removed it. Native routing (path 1) is the intended, supported approach.
- Less custom code. The generated middleware reimplements request forwarding that Nitro's native file-based routing already handles. Removing it drops custom code that duplicates framework functionality and shrinks the request-handling surface.
- Maintenance. Removing the option collapses three code paths into one and eliminates a generated virtual module along with its dev/build/prerender wiring.
Proposed change
- Remove the
useAPIMiddleware option from Options (options.ts).
- Remove the
apiMiddleware template (utils/renderers.ts) and the #ANALOG_API_MIDDLEWARE handler/virtual-module registration across the dev, build, and prerender paths.
- Remove the
useAPIMiddleware === false routeRules fallback.
- API routes are served solely via
src/server/routes/api (native Nitro file-based routing).
- Update docs and remove references to
useAPIMiddleware.
Migration
Projects that relied on the middleware (no src/server/routes/api directory, useAPIMiddleware left on) move their API handlers into src/server/routes/api — i.e. adopt the default scaffold layout. This is standard Nitro file-based routing and is already how new Analog apps are generated.
Breaking change
Yes — this removes a public option and a default behavior. Target a major release (the alpha line). Include a migration note in the release/changelog.
Alternatives considered
- Keep it deprecated indefinitely. Retains custom forwarding code and a second routing path for no functional gain over native routing.
- Flip the default to
false. Still ships and maintains the option and both fallback paths; doesn't remove the redundant code.
Summary
Remove the deprecated
useAPIMiddlewareoption and the generated#ANALOG_API_MIDDLEWAREproxy middleware from@analogjs/vite-plugin-nitro. API routes should be served exclusively through Nitro's native file-based routing undersrc/server/routes/api.Background
@analogjs/vite-plugin-nitrocurrently supports three ways of handling the/apiprefix (packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts):hasAPIDir === true— asrc/server/routes/apidirectory exists; Nitro serves those routes natively. (Recommended, and the default for every scaffold.)hasAPIDir === false && useAPIMiddleware === true— the default when no api directory exists. Injects the generated#ANALOG_API_MIDDLEWARE, which matches the/apiprefix and forwards the stripped request to$fetch/proxyRequest(fetchWithEvent/proxyRequeston the alpha line).hasAPIDir === false && useAPIMiddleware === false— falls back to a NitrorouteRulesproxy ({ '/api/**': { proxy: { to: '/**' } } }).useAPIMiddlewareis already marked@deprecatedinoptions.ts, with guidance to use thesrc/server/routes/apifolder instead.Motivation
create-analogtemplates and both@analogjs/nx-plugingenerators) already ships a populatedsrc/server/routes/apidirectory, so path 2 is only reached by projects that removed it. Native routing (path 1) is the intended, supported approach.Proposed change
useAPIMiddlewareoption fromOptions(options.ts).apiMiddlewaretemplate (utils/renderers.ts) and the#ANALOG_API_MIDDLEWAREhandler/virtual-module registration across the dev, build, and prerender paths.useAPIMiddleware === falserouteRulesfallback.src/server/routes/api(native Nitro file-based routing).useAPIMiddleware.Migration
Projects that relied on the middleware (no
src/server/routes/apidirectory,useAPIMiddlewareleft on) move their API handlers intosrc/server/routes/api— i.e. adopt the default scaffold layout. This is standard Nitro file-based routing and is already how new Analog apps are generated.Breaking change
Yes — this removes a public option and a default behavior. Target a major release (the alpha line). Include a migration note in the release/changelog.
Alternatives considered
false. Still ships and maintains the option and both fallback paths; doesn't remove the redundant code.