Skip to content

Commit 3b5f8b0

Browse files
committed
fix(utils): revert trailing slash preservation in joinURL
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.
1 parent c68013c commit 3b5f8b0

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

‎src/_utils.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ export function joinURL(base: string | undefined, path: string | undefined): str
194194
if (!base || base === "/") {
195195
return path || "/";
196196
}
197-
if (!path) {
198-
return base;
197+
if (!path || path === "/") {
198+
return base || "/";
199199
}
200200
// eslint-disable-next-line unicorn/prefer-at
201201
const baseHasTrailing = base[base.length - 1] === "/";

‎test/_utils.test.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,19 @@ describe("lib/http-proxy/common.js", () => {
316316
expect(outgoing.path).to.eql("/forward/static/path");
317317
});
318318

319+
it("should not add trailing slash when target has base path and request path is root", () => {
320+
const outgoing = createOutgoing();
321+
common.setupOutgoing(
322+
outgoing,
323+
{
324+
target: URL.parse("http://localhost/api")!,
325+
},
326+
stubIncomingMessage({ url: "/" }),
327+
);
328+
329+
expect(outgoing.path).to.eql("/api");
330+
});
331+
319332
it("should preserve multiple consecutive slashes in path (#80)", () => {
320333
const outgoing = createOutgoing();
321334
common.setupOutgoing(
@@ -693,14 +706,6 @@ describe("lib/http-proxy/common.js", () => {
693706
it("should concat when base has trailing slash and path has no leading slash", () => {
694707
expect(common.joinURL("/base/", "path")).to.eql("/base/path");
695708
});
696-
697-
it("should preserve trailing slash when path is exactly /", () => {
698-
expect(common.joinURL("/maildev", "/")).to.eql("/maildev/");
699-
});
700-
701-
it("should keep a single trailing slash when both base ends and path is /", () => {
702-
expect(common.joinURL("/maildev/", "/")).to.eql("/maildev/");
703-
});
704709
});
705710

706711
describe("#rewriteCookieProperty", () => {

0 commit comments

Comments
 (0)