Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: universal link redirects to general (#43131)

Jordi Enric9c8a8d25d1d9
diff --git a/apps/studio/components/interfaces/Organization/Organization.utils.ts b/apps/studio/components/interfaces/Organization/Organization.utils.ts
index e3b50819397..338b1b255f6 100644
--- a/apps/studio/components/interfaces/Organization/Organization.utils.ts
+++ b/apps/studio/components/interfaces/Organization/Organization.utils.ts
@@ -1,3 +1,27 @@
+/**
+ * Builds an org-scoped URL for the wildcard org route (/org/_).
+ *
+ * When `slug` is undefined the destination is the org project list at
+ * /org/<orgSlug>. When `slug` is an array the sub-path is preserved so the
+ * user lands on the equivalent page for the chosen org.
+ */
+export function buildOrgUrl({
+ slug,
+ orgSlug,
+ queryString,
+}: {
+ slug: string | string[] | undefined
+ orgSlug: string
+ queryString: string
+}): string {
+ const qs = queryString ? `?${queryString}` : ''
+ if (!Array.isArray(slug)) {
+ return `/org/${orgSlug}${qs}`
+ }
+ const slugPath = slug.reduce((a: string, b: string) => `${a}/${b}`, '').slice(1)
+ return `/org/${orgSlug}/${slugPath}${qs}`
+}
+
// Invite is expired if older than 24hrs
export function isInviteExpired(timestamp: string) {
const inviteDate = new Date(timestamp)
diff --git a/apps/studio/pages/org/_/[[...routeSlug]].tsx b/apps/studio/pages/org/_/[[...routeSlug]].tsx
index 2039e4e9ac8..a8bf05ab446 100644
--- a/apps/studio/pages/org/_/[[...routeSlug]].tsx
+++ b/apps/studio/pages/org/_/[[...routeSlug]].tsx
@@ -3,6 +3,7 @@ import {
LoadingCardView,
NoOrganizationsState,
} from 'components/interfaces/Home/ProjectList/EmptyStates'
+import { buildOrgUrl } from 'components/interfaces/Organization/Organization.utils'
import { PageLayout } from 'components/layouts/PageLayout/PageLayout'
import { ScaffoldContainer, ScaffoldSection } from 'components/layouts/Scaffold'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
@@ -23,17 +24,6 @@ const GenericOrganizationPage: NextPage = () => {
const { data: organizations, isPending: isLoading } = useOrganizationsQuery()
- const urlRewriterFactory = (slug: string | string[] | undefined) => {
- return (orgSlug: string) => {
- if (!Array.isArray(slug)) {
- return `/org/${orgSlug}/general${!!queryString ? `?${queryString}` : ''}`
- } else {
- const slugPath = slug.reduce((a: string, b: string) => `${a}/${b}`, '').slice(1)
- return `/org/${orgSlug}/${slugPath}${!!queryString ? `?${queryString}` : ''}`
- }
- }
- }
-
return (
<>
<Header />
@@ -60,7 +50,7 @@ const GenericOrganizationPage: NextPage = () => {
<OrganizationCard
key={org.id}
organization={org}
- href={urlRewriterFactory(routeSlug)(org.slug)}
+ href={buildOrgUrl({ slug: routeSlug, orgSlug: org.slug, queryString })}
/>
))}
</ul>
diff --git a/apps/studio/tests/pages/org/routeSlug.utils.test.ts b/apps/studio/tests/pages/org/routeSlug.utils.test.ts
new file mode 100644
index 00000000000..90dc6bbf2c7
--- /dev/null
+++ b/apps/studio/tests/pages/org/routeSlug.utils.test.ts
@@ -0,0 +1,64 @@
+import { buildOrgUrl } from 'components/interfaces/Organization/Organization.utils'
+import { describe, expect, test } from 'vitest'
+
+describe('buildOrgUrl', () => {
+ describe('when slug is undefined (bare /org/_ route)', () => {
+ test('returns the org project list url', () => {
+ expect(buildOrgUrl({ slug: undefined, orgSlug: 'my-org', queryString: '' })).toBe(
+ '/org/my-org'
+ )
+ })
+
+ test('appends query string when present', () => {
+ expect(buildOrgUrl({ slug: undefined, orgSlug: 'my-org', queryString: 'foo=bar' })).toBe(
+ '/org/my-org?foo=bar'
+ )
+ })
+
+ test('does not append a bare ? when query string is empty', () => {
+ expect(buildOrgUrl({ slug: undefined, orgSlug: 'my-org', queryString: '' })).not.toContain(
+ '?'
+ )
+ })
+ })
+
+ describe('when slug is a string (next.js router quirk — single segment)', () => {
+ test('returns the org project list url, ignoring the string slug', () => {
+ expect(buildOrgUrl({ slug: 'general', orgSlug: 'my-org', queryString: '' })).toBe(
+ '/org/my-org'
+ )
+ })
+
+ test('appends query string when present', () => {
+ expect(buildOrgUrl({ slug: 'general', orgSlug: 'my-org', queryString: 'ref=abc' })).toBe(
+ '/org/my-org?ref=abc'
+ )
+ })
+ })
+
+ describe('when slug is an array (sub-path route)', () => {
+ test('preserves a single-segment sub-path', () => {
+ expect(buildOrgUrl({ slug: ['general'], orgSlug: 'my-org', queryString: '' })).toBe(
+ '/org/my-org/general'
+ )
+ })
+
+ test('preserves a multi-segment sub-path', () => {
+ expect(
+ buildOrgUrl({ slug: ['settings', 'billing'], orgSlug: 'my-org', queryString: '' })
+ ).toBe('/org/my-org/settings/billing')
+ })
+
+ test('appends query string when present', () => {
+ expect(
+ buildOrgUrl({ slug: ['general'], orgSlug: 'my-org', queryString: 'foo=1&bar=2' })
+ ).toBe('/org/my-org/general?foo=1&bar=2')
+ })
+
+ test('does not append a bare ? when query string is empty', () => {
+ expect(buildOrgUrl({ slug: ['general'], orgSlug: 'my-org', queryString: '' })).not.toContain(
+ '?'
+ )
+ })
+ })
+})