Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(navigation): send the id-less /project path home instead of a 404 (#97089)

posthog[bot]bc835c215d26
diff --git a/frontend/src/lib/utils/kea-router.test.ts b/frontend/src/lib/utils/kea-router.test.ts
index a3a19dd5362f..350bb97e7952 100644
--- a/frontend/src/lib/utils/kea-router.test.ts
+++ b/frontend/src/lib/utils/kea-router.test.ts
@@ -1,4 +1,9 @@
-import { addProjectIdIfMissing, ensureRoutablePathname, stripTrailingSlash } from 'lib/utils/kea-router'
+import {
+ addProjectIdIfMissing,
+ ensureRoutablePathname,
+ removeProjectIdIfPresent,
+ stripTrailingSlash,
+} from 'lib/utils/kea-router'
describe('router-utils', () => {
it('does not redirect account URLs to a project URL', () => {
@@ -42,6 +47,29 @@ describe('router-utils', () => {
expect(addProjectIdIfMissing('/billing/authorization_status', 123)).toEqual('/billing/authorization_status')
})
+ describe('the id-less /project root', () => {
+ // An id-less `/project` used to survive the strip pass, match no route, and then get the
+ // current team prefixed onto it, so the address bar read `/project/<team id>/project`
+ // behind the 404 scene. Both forms must reduce to the root path instead.
+ it.each([
+ ['/project', '/'],
+ ['/project/', '/'],
+ ['/project?next=/home', '/?next=/home'],
+ // The URL the bug itself produced, so bookmarks of it have to land home too
+ ['/project/123/project', '/'],
+ // These have their own redirects, so stripping the prefix would send them to the
+ // unrelated /new and /settings scenes
+ ['/project/new', '/project/new'],
+ ['/project/settings', '/project/settings'],
+ ])('reduces %s to %s', (path, expected) => {
+ expect(removeProjectIdIfPresent(path)).toEqual(expected)
+ })
+
+ it.each(['/project', '/project/'])('sends %s to the current project root', (path) => {
+ expect(addProjectIdIfMissing(path, 123)).toEqual('/project/123')
+ })
+ })
+
describe('relative path normalization', () => {
it('normalizes ../ prefix to absolute path with project id', () => {
expect(addProjectIdIfMissing('../dashboard/1663553', 112509)).toEqual('/project/112509/dashboard/1663553')
diff --git a/frontend/src/lib/utils/kea-router.ts b/frontend/src/lib/utils/kea-router.ts
index 3fa8d697b9f5..b88e3af75f7d 100644
--- a/frontend/src/lib/utils/kea-router.ts
+++ b/frontend/src/lib/utils/kea-router.ts
@@ -30,6 +30,14 @@ const exactPathsWithoutProjectId = ['/feature_flags/staff', '/experiments/staff'
const projectIdentifierInUrlRegex = /^\/project\/(\d+|phc_)/
+// `/project` and `/project/` carry no id, so the regex above does not see a project prefix there.
+// Without this, the path survives the strip pass, matches no route, and renders the 404 scene,
+// while `addProjectIdUnlessPresent` prefixes the current team onto it and rewrites the address bar
+// to `/project/<team id>/project`. That rewritten URL reduces to the same bare form once its id is
+// stripped, so bookmarks of it land home too. The lookahead keeps real routes like `/project/new`
+// and `/project/settings` out, because they have their own entries in the redirects map.
+const projectRootWithoutIdentifierInUrlRegex = /^\/project\/?(?=$|[?#])/
+
function isPathWithoutProjectId(path: string): boolean {
const pathname = path.split(/[?#]/)[0]
if (
@@ -55,6 +63,8 @@ function addProjectIdUnlessPresent(path: string, teamId?: TeamType['id']): strin
path = normalizeRelativePath(path)
}
+ path = path.replace(projectRootWithoutIdentifierInUrlRegex, '/')
+
let prefix = ''
try {
prefix = `/project/${teamId ?? getCurrentTeamId()}`
@@ -71,10 +81,8 @@ function addProjectIdUnlessPresent(path: string, teamId?: TeamType['id']): strin
}
export function removeProjectIdIfPresent(path: string): string {
- if (path.match(projectIdentifierInUrlRegex)) {
- return '/' + path.split('/').splice(3).join('/')
- }
- return path
+ const withoutProjectId = path.match(projectIdentifierInUrlRegex) ? '/' + path.split('/').splice(3).join('/') : path
+ return withoutProjectId.replace(projectRootWithoutIdentifierInUrlRegex, '/')
}
/**
diff --git a/frontend/src/scenes/sceneLogic.test.tsx b/frontend/src/scenes/sceneLogic.test.tsx
index c6b18a70b77f..d82a5941df19 100644
--- a/frontend/src/scenes/sceneLogic.test.tsx
+++ b/frontend/src/scenes/sceneLogic.test.tsx
@@ -99,6 +99,12 @@ describe('sceneLogic', () => {
expect(removeProjectIdIfPresent(router.values.location.pathname)).toEqual(urls.projectCreateFirst())
})
+ it.each(['/project', '/project/'])('sends the id-less %s path to the homepage, not a 404', async (path) => {
+ router.actions.push(path)
+ await expectLogic(logic).delay(1)
+ expect(removeProjectIdIfPresent(router.values.location.pathname)).toEqual(urls.projectHomepage())
+ })
+
it('redirects /data-warehouse/new to the new-source wizard instead of a 404', async () => {
router.actions.push('/data-warehouse/new')
await expectLogic(logic).delay(1)