Skip to content

fix(router): only strip basePath when it is a leading prefix#149

Merged
bytaesu merged 2 commits into
mainfrom
fix/router-basepath-leading-prefix
Jun 24, 2026
Merged

fix(router): only strip basePath when it is a leading prefix#149
bytaesu merged 2 commits into
mainfrom
fix/router-basepath-leading-prefix

Conversation

@Bekacru

@Bekacru Bekacru commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

The router derived the route path by stripping basePath wherever it occurred in the request pathname (pathname.split(basePath)). That dropped any segments before the first occurrence of basePath, so a pathname like /x/api/test resolved onto the internal route /test even though it was not mounted under basePath.

This changes the router to strip basePath only when it is a leading, /-boundary prefix of the request pathname. A pathname that does not start with the configured basePath now resolves to a 404. Trailing slashes on basePath are normalized so /api/auth/ and /api/auth behave the same.

Behavior change

  • Requests where basePath is not a leading prefix (e.g. /x/api/test with basePath: "/api") now return 404 instead of being routed.
  • Normal requests under basePath are unchanged.
  • A basePath of / (or unset) is unchanged.

Tests

Added router tests for:

  • a pathname where basePath is not a leading prefix → 404
  • the handler not being invoked when basePath appears after another segment
  • basePath with a trailing slash being normalized

Full suite green (183 tests).

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.
Copilot AI review requested due to automatic review settings June 20, 2026 22:39
@pkg-pr-new

pkg-pr-new Bot commented Jun 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/better-call@149

commit: b99a88f

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 on basePath.
  • Added tests to ensure non-leading basePath occurrences return 404 and that trailing-slash basePath values 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.

Comment thread packages/better-call/src/router.ts Outdated
@bytaesu bytaesu added the backport v1.3.x Backport this PR to v1.3.x branch label Jun 24, 2026
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.
@bytaesu bytaesu force-pushed the fix/router-basepath-leading-prefix branch from 45ec304 to b99a88f Compare June 24, 2026 22:50
@bytaesu bytaesu merged commit de59505 into main Jun 24, 2026
5 checks passed
@better-release

Copy link
Copy Markdown

Successfully created backport PR for v1.3.x:

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport v1.3.x Backport this PR to v1.3.x branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants