feat: ignore trailing path segments after the parsable portion of a URL#139
Conversation
Adds a rewriteUrl hook that progressively strips trailing path segments when the original URL only matches the static catch-all wildcard or no route at all, so `/status/429/foo` is served by the `/status/:code` handler instead of returning 404. Fixes #137 https://claude.ai/code/session_01V1j124Md6c76NjGTzHznmL
There was a problem hiding this comment.
Code Review
This pull request introduces a URL rewriting mechanism for Fastify that allows the server to handle requests with extra trailing path segments by iteratively stripping them until a matching route is found. It includes a helper function to distinguish specific routes from wildcards and provides comprehensive unit tests. The review feedback identifies opportunities to optimize the initial route check by extracting the path from the query string earlier and suggests refining the segment-stripping loop to correctly handle edge cases like trailing slashes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #139 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 37 37
Lines 1063 1089 +26
Branches 211 222 +11
=========================================
+ Hits 1063 1089 +26 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…ling-slash paths Strips the query string first so the initial findRoute check sees the real path. Also checks the segments-as-is at the top of the loop so URLs like `/status/429/` collapse to `/status/429` rather than `/status`. https://claude.ai/code/session_01V1j124Md6c76NjGTzHznmL
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6f82897bef
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Summary
rewriteUrlhook insrc/fastify-config.tsthat, when a request URL doesn't match a specific route (only the static catch-all wildcard or nothing), progressively strips trailing path segments and rewrites to the first specific route prefix that matches./status/429/foo(or/status/429/foo/bar/) is now served by the/status/:codehandler with status code 429, just like/status/429././*) is treated as "no specific match", so a stripped candidate that matches a real route wins over the wildcard. Otherwise the original URL is returned and the wildcard still serves it.Test plan
pnpm test(412 tests pass, 100% line coverage;src/fastify-config.tsfully covered)pnpm lintpnpm buildtest/fastify-config.test.tscover: trailing segment(s), trailing slash, query string preservation, method matching, no-rewrite when URL already matches, 404 on completely unknown paths, no-strip-to-root, missing method, missing URL, and wildcard-vs-specific precedence.test/mock-http.test.tsconfirms/status/429/fooreturns 429 and/totally/unknown/pathstill returns 404 on a fullMockHttpinstance.https://claude.ai/code/session_01V1j124Md6c76NjGTzHznmL
Generated by Claude Code