fix(router): only strip basePath when it is a leading prefix#149
Merged
Conversation
The router derived the route path by stripping basePath wherever it appeared via pathname.split(basePath), which dropped any segments before the first occurrence. A pathname like /x/api/test would then resolve onto the internal route /test even though it is not mounted under basePath. Strip basePath only when it is a leading, /-boundary prefix of the request pathname; pathnames outside basePath now resolve to a 404. Trailing slashes on basePath are normalized so /api/auth/ and /api/auth behave the same.
commit: |
There was a problem hiding this comment.
Pull request overview
Adjusts createRouter’s basePath handling so routing only occurs when basePath is a true leading path-segment prefix of the request pathname, preventing accidental matches when basePath appears mid-path.
Changes:
- Replaced
pathname.split(basePath)-based stripping with a leading-prefix + boundary check, including normalization of trailing slashes onbasePath. - Added tests to ensure non-leading
basePathoccurrences return404and that trailing-slashbasePathvalues behave consistently.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/better-call/src/router.ts | Updates basePath stripping logic to only apply for leading, path-segment-prefixed matches (and normalizes trailing slashes). |
| packages/better-call/src/router.test.ts | Adds regression tests covering mid-path basePath occurrences and basePath trailing-slash normalization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Compute the trailing-slash-normalized base path once at router creation instead of on every request, and resolve the request path with guard-clause early returns. Behavior is unchanged. Add a regression test for a sibling prefix that matches without a "/" boundary.
45ec304 to
b99a88f
Compare
|
Successfully created backport PR for |
bytaesu
added a commit
that referenced
this pull request
Jun 24, 2026
…152) * fix(router): only strip basePath when it is a leading prefix The router derived the route path by stripping basePath wherever it appeared via pathname.split(basePath), which dropped any segments before the first occurrence. A pathname like /x/api/test would then resolve onto the internal route /test even though it is not mounted under basePath. Strip basePath only when it is a leading, /-boundary prefix of the request pathname; pathnames outside basePath now resolve to a 404. Trailing slashes on basePath are normalized so /api/auth/ and /api/auth behave the same. * refactor(router): derive base path once and flatten path resolution Compute the trailing-slash-normalized base path once at router creation instead of on every request, and resolve the request path with guard-clause early returns. Behavior is unchanged. Add a regression test for a sibling prefix that matches without a "/" boundary. --------- (cherry picked from commit de59505) Co-authored-by: Bereket Engida <86073083+Bekacru@users.noreply.github.com> Co-authored-by: Taesu <bytaesu@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The router derived the route path by stripping
basePathwherever it occurred in the request pathname (pathname.split(basePath)). That dropped any segments before the first occurrence ofbasePath, so a pathname like/x/api/testresolved onto the internal route/testeven though it was not mounted underbasePath.This changes the router to strip
basePathonly when it is a leading,/-boundary prefix of the request pathname. A pathname that does not start with the configuredbasePathnow resolves to a404. Trailing slashes onbasePathare normalized so/api/auth/and/api/authbehave the same.Behavior change
basePathis not a leading prefix (e.g./x/api/testwithbasePath: "/api") now return404instead of being routed.basePathare unchanged.basePathof/(or unset) is unchanged.Tests
Added router tests for:
basePathis not a leading prefix →404basePathappears after another segmentbasePathwith a trailing slash being normalizedFull suite green (183 tests).