Skip to content

fix: mount(handler) with prefix should strip prefix from path#1811

Open
rafia9005 wants to merge 1 commit intoelysiajs:mainfrom
rafia9005:fix/mount-prefix-path-stripping
Open

fix: mount(handler) with prefix should strip prefix from path#1811
rafia9005 wants to merge 1 commit intoelysiajs:mainfrom
rafia9005:fix/mount-prefix-path-stripping

Conversation

@rafia9005
Copy link
Copy Markdown

@rafia9005 rafia9005 commented Mar 20, 2026

Summary

Fixes #1806

When using mount(handler) without an explicit path on an Elysia instance with a prefix (e.g., new Elysia({ prefix: '/api' }).mount(handler)), the mounted handler was receiving the full path including the prefix instead of having the prefix stripped.

Problem

const api = new Elysia({ prefix: '/api' }).mount(authHandler)
const app = new Elysia().use(api)

// Request: /api/auth/dash/users
// Before fix: handler received /api/auth/dash/users
// After fix: handler received /auth/dash/users

This caused issues when integrating libraries like Better Auth which expect the path to start from their basePath, not from the full URL path.

Changes

  • Modified the mount() method to strip the prefix from the path before passing it to the mounted handler (when no explicit path is provided)
  • Added test cases to verify the fix

Test Results

All 1527 tests pass, including 2 new test cases for this fix.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed path prefix handling in mounted plugins to correctly strip prefixes from request paths.
  • Tests

    • Added test cases verifying prefix stripping behavior for mounted routes under different path configurations.

When using mount(handler) without an explicit path on an Elysia instance
with a prefix (e.g., new Elysia({ prefix: '/api' }).mount(handler)), the
mounted handler was receiving the full path including the prefix instead
of having the prefix stripped.

This caused issues when integrating libraries like Better Auth which
expect the path to start from their basePath, not from the full URL path.

Fixes elysiajs#1806
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 68e086c4-36da-4ea2-b429-2fe63b51d25f

📥 Commits

Reviewing files that changed from the base of the PR and between 56310be and 1675060.

📒 Files selected for processing (2)
  • src/index.ts
  • test/core/mount.test.ts

Walkthrough

Updated the mount() method in src/index.ts to correctly handle URL path stripping when mounting plugins with prefixes. The change derives the prefix from this.config.prefix and adjusts the path accordingly. Added two test cases validating prefix stripping behavior for GitHub issue #1806.

Changes

Cohort / File(s) Summary
Mount Prefix Handling
src/index.ts
Updated request path handling in mount()'s fast-path to derive prefix and prefixLength from this.config.prefix, then slice the request path to remove the prefix before passing it to replaceUrlPath(). Also includes minor reformatting of imports and boolean expression expansion.
Mount Prefix Tests
test/core/mount.test.ts
Added two test cases for issue #1806 verifying that plugin-mounted paths correctly strip the prefix in root and nested path scenarios.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #1657: Modifies mount request path handling and prefix stripping in src/index.ts to fix incorrect URL path behavior when an Elysia instance has a prefix.

Poem

🐰 With prefix paths now stripped with care,
The rabbit hops through mounted layers fair,
No stray /api clutters the request,
Each path now routed to its nested best!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing the mount() method to properly strip prefix from path when a prefix is configured.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can generate walkthrough in a markdown collapsible section to save space.

Enable the reviews.collapse_walkthrough setting to generate walkthrough in a markdown collapsible section.

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.

Elysia.mount(auth.handler) returns 404 for /api/auth/* but works when using explicit GET/POST routes

1 participant