Skip to content

Remove deprecated useAPIMiddleware and the generated API proxy middleware #2429

Description

@brandonroberts

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):

  1. hasAPIDir === true — a src/server/routes/api directory exists; Nitro serves those routes natively. (Recommended, and the default for every scaffold.)
  2. 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).
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions