Skip to content

fix: preserve trailing slash when proxying root path to a sub-path target - #137

Merged
pi0 merged 1 commit into
unjs:mainfrom
SAY-5:fix/joinurl-preserve-root-slash-134
May 19, 2026
Merged

pi0 merged 1 commit into
unjs:mainfrom
SAY-5:fix/joinurl-preserve-root-slash-134

Conversation

@SAY-5

@SAY-5 SAY-5 commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #134. The path === "/" early-return dropped the slash so joinURL("/maildev", "/") returned "/maildev" instead of "/maildev/", causing redirect loops with sub-path-mounted upstreams. Treat root-path as a real path and let the trailing/leading slash logic produce the correct join. Per @pi0 in #134 ("PR welcome!").

Summary by CodeRabbit

  • Bug Fixes

    • Fixed URL path joining to correctly preserve single trailing slashes in certain cases.
  • Tests

    • Added tests to verify trailing-slash handling in URL path operations.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 13, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The joinURL function is refined to handle trailing slashes more precisely. Previously, the function treated both falsy path and path === "/" as early-return cases; now only falsy path triggers early return, allowing "/" to be joined normally. Two test cases verify the behavior for both a standalone "/" path and the case where both base and path end with "/".

Changes

Trailing slash handling in joinURL

Layer / File(s) Summary
joinURL trailing slash logic and validation
src/_utils.ts, test/_utils.test.ts
The early-return condition in joinURL is narrowed to only trigger on falsy path, allowing path === "/" to proceed through normal joining logic. Two new test cases validate that "/" paths are joined correctly and that multiple trailing slashes are collapsed to a single slash.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Possibly related issues

Poem

🐰 A slash was treated like a ghost,
But now it joins the joining post.
With logic refined, the path flows true,
Single slashes shine right through! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title accurately describes the main change: fixing trailing slash preservation when joining URLs with root path, which matches the core bug fix in the changeset.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@pi0 pi0 changed the title fix: preserve trailing slash in joinURL when path is "/" (#134) fix: preserve trailing slash in joinURL when path is / May 19, 2026

@pi0 pi0 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.

thnx!

@pi0 pi0 changed the title fix: preserve trailing slash in joinURL when path is / fix: preserve trailing slash when proxying root path to a sub-path target May 19, 2026
@pi0
pi0 merged commit 5cb386e into unjs:main May 19, 2026
2 checks passed
@codecov

codecov Bot commented May 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.91%. Comparing base (9dc2425) to head (79703f8).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #137   +/-   ##
=======================================
  Coverage   94.91%   94.91%           
=======================================
  Files           8        8           
  Lines         786      786           
  Branches      320      319    -1     
=======================================
  Hits          746      746           
  Misses         35       35           
  Partials        5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

pi0 added a commit that referenced this pull request May 20, 2026
Reverts #137 to restore the pre-fix path behavior, since downstream consumers (http-proxy-middleware v4, webpack-dev-server, @rspack/dev-server) rely on `joinURL("/api", "/")` returning `"/api"` rather than `"/api/"`.

Adds a regression test guarding `setupOutgoing` so the previous behavior stays locked in.
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.

joinURL drops trailing slash when path is "/", breaks proxying to apps mounted on a sub-path

2 participants