Skip to content

fix: skip routes already registered by other plugins - #487

Merged
Tony133 merged 5 commits into
mainfrom
fix/309-cors-options
Oct 19, 2026
Merged

Tony133 merged 5 commits into
mainfrom
fix/309-cors-options

Conversation

@Tony133

@Tony133 Tony133 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Proposal:

Registering @fastify/http-proxy on the root together with @fastify/cors fails with:

    Method 'OPTIONS' already declared for route '/*'

because both plugins declare an OPTIONS /* route.

Changes:

  • Before registering each proxied route, the plugin now skips the methods already registered for that url (checked with fastify.hasRoute). Preflight requests are then answered by @fastify/cors, and every other request is still proxied.
  • The skipped methods are reported with a debug log entry ({ url, methods }), so that an unintended overlap between a local route and the proxy is still visible without turning the legitimate @fastify/cors case into a boot-time warning.
  • If no OPTIONS route exists, OPTIONS requests are still proxied upstream as before, so current users see no change.
  • README: explain that plugins like @fastify/cors must be registered before the proxy (or OPTIONS removed from httpMethods otherwise), and mention the debug log entry.

Tests:

  • New test/existing-routes.js:

    • existing OPTIONS routes are kept and the other methods are proxied
    • OPTIONS is still proxied when no route handles it
    • a route whose methods are all already registered is skipped
    • httpMethods passed as a string
    • prefix alongside a global OPTIONS /* route
    • the skipped methods are logged at debug level with the expected url and methods

The tests register the same routes as @fastify/cors by hand, so no new dev dependency is needed. I also checked the fix manually against @fastify/cors 11.3.0.

Fixes #309

@Tony133
Tony133 marked this pull request as ready for review September 28, 2026 09:05
@Tony133
Tony133 requested a review from a team September 28, 2026 12:32
@Tony133
Tony133 requested a review from a team October 3, 2026 13:31
Comment thread index.js Outdated
Comment thread index.js Outdated
Comment thread index.js Outdated
@Tony133
Tony133 force-pushed the fix/309-cors-options branch from 4c4b192 to 7893ab1 Compare October 5, 2026 15:54
@gurgunday
gurgunday requested a review from Eomm October 5, 2026 19:46

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Tony133
Tony133 enabled auto-merge (squash) October 19, 2026 21:25
@Tony133
Tony133 merged commit efe18ef into main Oct 19, 2026
20 checks passed
@Tony133
Tony133 deleted the fix/309-cors-options branch October 19, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to use http-proxy for all routes (/*) with @fastify/cors plugin

4 participants