Skip to content

Commit e0719bb

Browse files
committed
fix(webapp): match the environment path on a segment boundary
The page below the environment was sliced off the front of the pathname on a bare startsWith, so an environment slug that strictly prefixes another — branch slugs are `<parent>-<branch>`, making `preview-feat` a prefix of `preview-feat-2` — yielded a garbled suffix that resolved to no page. The switcher hooks can hit this mid-navigation, where the pathname comes from the pending location but the environment path still comes from the current match. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2d2c417 commit e0719bb

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

apps/webapp/app/utils/pageSwitching.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ describe("pageBelowEnvironment", () => {
437437
expect(pageBelowEnvironment("/account/tokens", environmentLocation.pathname)).toBe("");
438438
expect(pageBelowEnvironment("/orgs/acme/settings/team", environmentLocation.pathname)).toBe("");
439439
});
440+
441+
it("gives nothing when the environment path only prefixes the one in the page path", () => {
442+
const branch = "/orgs/acme/projects/api/env/preview-feat";
443+
444+
expect(pageBelowEnvironment(`${branch}-2/runs`, branch)).toBe("");
445+
expect(pageBelowEnvironment(`${branch}-2`, branch)).toBe("");
446+
expect(pageBelowEnvironment(`${branch}/runs`, branch)).toBe("runs");
447+
});
440448
});
441449

442450
describe("pathForEnvironmentSwitch", () => {

apps/webapp/app/utils/pageSwitching.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ export function pageBelowEnvironment(
101101
): string {
102102
if (environmentPathname === undefined || !pathname.startsWith(environmentPathname)) return "";
103103

104-
return pathname.slice(environmentPathname.length).replace(/^\/+/, "");
104+
const below = pathname.slice(environmentPathname.length);
105+
if (below !== "" && !below.startsWith("/")) return "";
106+
107+
return below.replace(/^\/+/, "");
105108
}
106109

107110
/** The current page in another environment of the same project, keeping filters where they apply. */

0 commit comments

Comments
 (0)