mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Removes the remaining Next.js dependencies, compatibility shims, test
mocks, ESLint/TypeScript presets, and webpack/Next workarounds from
Studio following the TanStack Start migration.
Addresses FE-3107.
**Removed:**
- `next` and `next-router-mock` from `apps/studio/package.json` (and
updated `pnpm-lock.yaml`)
- Orphaned `apps/studio/compat/next/script.tsx` shim, `script.test.tsx`,
`compat/**` knip entry, and `nextCompat()` / `ssr.noExternal` `/^next/`
plumbing from `apps/studio/vite.config.ts`
- `apps/studio/tests/lib/route-mock.ts` and all `next-router-mock`
setup/imports across `tests/vitestSetup.ts`,
`tests/lib/custom-render.tsx`, and 12 test files
- `apps/studio/tests/pages/` directory (moved the 3 remaining tests to
`components/interfaces/Organization/Organization.utils.test.ts` and
`apps/studio/tests/routes/`)
- Unused `'use client'` directives across 23 Studio files
- Next.js/webpack workarounds: `graphiqlViteWorkers()` in
`vite.config.ts`, webpack `import.meta.glob` `try/catch` fallback in
`ConnectStepsSection.tsx`, shell `NODE_ENV=test` override in
`vite.config.ts` / root `package.json` / `studio-e2e-test.yml`, Next
`getPathMatch` and `_next` passthrough in `redirects.shared.*`,
`window.next?.router` check in `monaco-graphiql-coexistence.spec.ts`,
and unused `ANALYZE` env var in `apps/studio/turbo.jsonc`
- Next-only Sentry/chunk-load filters (`NEXT_NOT_FOUND`,
`NEXT_REDIRECT`, Next `/org/[slug]/` regex, `/app:\/\//`, dead
`includeThirdPartyErrorFilter` plumbing, webpack chunk error regex) and
stale Next/Pages Router comments, docs, and `#__next` CSS selector
**Changed:**
- `apps/studio/eslint.config.cjs`: switched from
`eslint-config-supabase/next` to the new `eslint-config-supabase/react`
preset (dropping `@next/next/no-img-element: off`, keeping `.next` /
`next-env.d.ts` ignored for local checkouts with stale pre-migration
build artifacts)
- `apps/studio/tsconfig.json`: extends `tsconfig/base.json` directly
instead of `tsconfig/nextjs.json` (identical resolved compiler options)
- `apps/studio/.github/eslint-rule-baselines.json`: pruned 33 stale
`pages/...` and `types/next.ts` entries without changing any rule
thresholds
- `apps/studio/tests/lib/custom-render.tsx`: backed
`TestFrameworkProvider` with TanStack Router instead of
`next-router-mock`
-
`apps/studio/components/interfaces/Integrations/GraphQL/GraphiQLTab.tsx`:
imports `graphiql/setup-workers/vite` directly
- `apps/studio/styles/globals.css`: replaced `@source
'../pages/**/*.{js,ts,jsx,tsx}'` with `@source
'../routes/**/*.{js,ts,jsx,tsx}'`
- `apps/studio/turbo.jsonc`: declared `"NEXT_PUBLIC_*"` in
`tasks.build.env` now that Turborepo infers `nitro` instead of `nextjs`
-
`apps/studio/components/interfaces/Organization/Organization.utils.ts`:
narrowed `buildOrgUrl`'s `slug` parameter to `string[] | undefined`
**Added:**
- `packages/eslint-config-supabase/shared.js` and
`packages/eslint-config-supabase/react.js` (plus `presets.test.ts`),
decoupling non-Next React apps from `eslint-config-next` with verified
zero non-`@next/*` rule drift while keeping
`eslint-config-supabase/next` unchanged for the Next.js apps
## To test
- Run `pnpm --filter eslint-config-supabase run test` and confirm preset
parity tests pass
- Run `pnpm --filter studio run lint`, `pnpm --filter studio run
lint:ratchet`, `pnpm --filter studio run typecheck`, `pnpm knip
--workspace apps/studio`, and `pnpm --filter studio run test`
- Run `pnpm --filter studio run build` and verify the production build,
prerendered routes, chunk-cycle check, and GraphiQL web workers succeed
- Smoke-test Studio locally (`pnpm dev:studio`): verify GraphiQL loads
cleanly, ConnectSheet framework steps render, and Tailwind styles on
route pages are intact
---------
Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
578 lines
19 KiB
TypeScript
578 lines
19 KiB
TypeScript
// Redirect tables consumed by `vercel.ts` (the Vercel edge) and
|
|
// `routes/__root.tsx` (`matchRedirect`, for the Node server and client-side
|
|
// navigation). Defined once here so the two don't drift.
|
|
//
|
|
// Shape mirrors Vercel's `Redirect` type (`source`, `destination`,
|
|
// `permanent`, optional `has`). Vercel doesn't apply the basePath, so
|
|
// `vercel.ts` prefixes it.
|
|
|
|
export interface StudioRedirect {
|
|
source: string
|
|
destination: string
|
|
permanent: boolean
|
|
has?: Array<{ type: 'query'; key: string; value: string }>
|
|
}
|
|
|
|
export const PLATFORM_REDIRECTS: StudioRedirect[] = [
|
|
{
|
|
source: '/',
|
|
has: [{ type: 'query', key: 'next', value: 'new-project' }],
|
|
destination: '/new/new-project',
|
|
permanent: false,
|
|
},
|
|
{ source: '/', destination: '/org', permanent: false },
|
|
{ source: '/register', destination: '/sign-up', permanent: false },
|
|
{ source: '/signup', destination: '/sign-up', permanent: false },
|
|
{ source: '/signin', destination: '/sign-in', permanent: false },
|
|
{ source: '/login', destination: '/sign-in', permanent: false },
|
|
{ source: '/log-in', destination: '/sign-in', permanent: false },
|
|
{ source: '/project/:ref/building', destination: '/project/:ref', permanent: false },
|
|
]
|
|
|
|
export const SELF_HOSTED_REDIRECTS: StudioRedirect[] = [
|
|
{ source: '/', destination: '/project/default', permanent: false },
|
|
{ source: '/register', destination: '/project/default', permanent: false },
|
|
{ source: '/signup', destination: '/project/default', permanent: false },
|
|
{ source: '/signin', destination: '/project/default', permanent: false },
|
|
{ source: '/login', destination: '/project/default', permanent: false },
|
|
{ source: '/log-in', destination: '/project/default', permanent: false },
|
|
{ source: '/project/:ref/building', destination: '/project/:ref', permanent: false },
|
|
]
|
|
|
|
export const SHARED_REDIRECTS: StudioRedirect[] = [
|
|
{ source: '/project', destination: '/organizations', permanent: true },
|
|
{ source: '/account', destination: '/account/me', permanent: true },
|
|
{ source: '/org/:slug/projects', destination: '/org/:slug', permanent: true },
|
|
{
|
|
source: '/project/:ref/database/backups',
|
|
destination: '/project/:ref/database/backups/scheduled',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/auth/emails',
|
|
destination: '/project/:ref/auth/templates',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/logs/edge-functions',
|
|
destination: '/project/:ref/logs/edge-functions-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/vault',
|
|
destination: '/project/:ref/integrations/vault/secrets',
|
|
permanent: true,
|
|
},
|
|
{ source: '/project/:ref/settings/billing', destination: '/org/_/billing', permanent: true },
|
|
{ source: '/project/:ref/auth', destination: '/project/:ref/auth/users', permanent: true },
|
|
{
|
|
source: '/project/:ref/auth/advanced',
|
|
destination: '/project/:ref/auth/performance',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/auth/policies',
|
|
destination: '/project/:ref/database/policies',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database',
|
|
destination: '/project/:ref/database/tables',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/graphiql',
|
|
destination: '/project/:ref/api/graphiql',
|
|
permanent: true,
|
|
},
|
|
{ source: '/project/:ref/storage', destination: '/project/:ref/storage/files', permanent: true },
|
|
{
|
|
source: '/project/:ref/storage/buckets',
|
|
destination: '/project/:ref/storage/files',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/storage/policies',
|
|
destination: '/project/:ref/storage/files/policies',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/storage/buckets/:bucketId',
|
|
destination: '/project/:ref/storage/files/buckets/:bucketId',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/api-keys/new',
|
|
destination: '/project/:ref/settings/api-keys',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/storage',
|
|
destination: '/project/:ref/storage/files/settings',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/storage/settings',
|
|
destination: '/project/:ref/storage/files/settings',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/database',
|
|
destination: '/project/:ref/database/settings',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings',
|
|
destination: '/project/:ref/settings/general',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/auth/settings',
|
|
destination: '/project/:ref/auth/providers',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/subscription',
|
|
has: [{ type: 'query', key: 'panel', value: 'subscriptionPlan' }],
|
|
destination: '/org/_/billing?panel=subscriptionPlan',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/compute-and-disk',
|
|
destination: '/project/:ref/settings/infrastructure',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/replication/replica/:replicaId',
|
|
destination: '/project/:ref/settings/infrastructure/replica/:replicaId',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/replication',
|
|
destination: '/project/:ref/database/pipelines',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/replication/:pipelineId',
|
|
destination: '/project/:ref/database/pipelines/:pipelineId',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/subscription',
|
|
has: [{ type: 'query', key: 'panel', value: 'pitr' }],
|
|
destination: '/project/:ref/settings/addons?panel=pitr',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/subscription',
|
|
has: [{ type: 'query', key: 'panel', value: 'computeInstance' }],
|
|
destination: '/project/:ref/settings/infrastructure#cpu',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/subscription',
|
|
has: [{ type: 'query', key: 'panel', value: 'customDomain' }],
|
|
destination: '/project/:ref/settings/addons?panel=customDomain',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/subscription',
|
|
destination: '/org/_/billing',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/jwt/signing-keys',
|
|
destination: '/project/:ref/settings/jwt',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/api-logs',
|
|
destination: '/project/:ref/logs/edge-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/postgres-logs',
|
|
destination: '/project/:ref/logs/postgres-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/postgrest-logs',
|
|
destination: '/project/:ref/logs/postgrest-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/pgbouncer-logs',
|
|
destination: '/project/:ref/logs/pooler-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/logs/pgbouncer-logs',
|
|
destination: '/project/:ref/logs/pooler-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/realtime-logs',
|
|
destination: '/project/:ref/logs/realtime-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/storage/logs',
|
|
destination: '/project/:ref/logs/storage-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/auth/logs',
|
|
destination: '/project/:ref/logs/auth-logs',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/logs-explorer',
|
|
destination: '/project/:ref/logs/explorer',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/logs/explorer/recent',
|
|
destination: '/project/:ref/logs/explorer',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/logs/explorer/saved',
|
|
destination: '/project/:ref/logs/explorer',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/sql/quickstarts',
|
|
destination: '/project/:ref/sql/examples',
|
|
permanent: true,
|
|
},
|
|
{ source: '/org/:slug/settings', destination: '/org/:slug/general', permanent: true },
|
|
{
|
|
source: '/project/:ref/settings/billing/update',
|
|
destination: '/org/_/billing',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/update/free',
|
|
destination: '/org/_/billing',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/update/pro',
|
|
destination: '/org/_/billing',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/update/team',
|
|
destination: '/org/_/billing',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/billing/update/enterprise',
|
|
destination: '/org/_/billing',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/reports/linter',
|
|
destination: '/project/:ref/database/linter',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/reports',
|
|
destination: '/project/:ref/observability',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/reports/:path*',
|
|
destination: '/project/:ref/observability/:path*',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/query-performance',
|
|
destination: '/project/:ref/observability/query-performance',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/advisors',
|
|
destination: '/project/:ref/advisors/security',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/advisors/query-performance',
|
|
destination: '/project/:ref/observability/query-performance',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/query-performance',
|
|
destination: '/project/:ref/observability/query-performance',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/auth/column-privileges',
|
|
destination: '/project/:ref/database/column-privileges',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/linter',
|
|
destination: '/project/:ref/advisors/security',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/security-advisor',
|
|
destination: '/project/:ref/advisors/security',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/performance-advisor',
|
|
destination: '/project/:ref/advisors/performance',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/webhooks',
|
|
destination: '/project/:ref/integrations/webhooks/overview',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/hooks',
|
|
destination: '/project/:ref/integrations/webhooks/overview',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/wrappers',
|
|
destination: '/project/:ref/integrations?category=wrapper',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/database/cron-jobs',
|
|
destination: '/project/:ref/integrations/cron',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/api/graphiql',
|
|
destination: '/project/:ref/integrations/graphiql',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/vault/secrets',
|
|
destination: '/project/:ref/integrations/vault/secrets',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/vault/keys',
|
|
destination: '/project/:ref/integrations/vault/keys',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/integrations/cron-jobs',
|
|
destination: '/project/:ref/integrations/cron',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/warehouse',
|
|
destination: '/project/:ref/settings/general',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/functions',
|
|
destination: '/project/:ref/functions/secrets',
|
|
permanent: true,
|
|
},
|
|
{ source: '/org/:slug/invoices', destination: '/org/:slug/billing#invoices', permanent: true },
|
|
{ source: '/projects', destination: '/organizations', permanent: false },
|
|
{
|
|
source: '/project/:ref/settings/auth',
|
|
destination: '/project/:ref/auth/providers',
|
|
permanent: true,
|
|
},
|
|
{
|
|
source: '/project/:ref/settings/api',
|
|
destination: '/project/:ref/integrations/data_api/overview',
|
|
permanent: false,
|
|
},
|
|
{
|
|
source: '/project/:ref/api',
|
|
destination: '/project/:ref/integrations/data_api/docs',
|
|
permanent: false,
|
|
},
|
|
]
|
|
|
|
// Top-level path segments (below the base path) that stay reachable in
|
|
// maintenance mode: the maintenance page itself plus every static file it may
|
|
// need to render. Page routes, `/api` and `/_serverFn` are still redirected —
|
|
// maintenance takes the dashboard's backend offline too, and the maintenance
|
|
// page doesn't depend on either.
|
|
export const MAINTENANCE_PASSTHROUGH_SEGMENTS = [
|
|
'maintenance',
|
|
// Build output. Vite's chunks are under `/assets`, or
|
|
// `/_vercel/immutable/<salt>/nitro` on Vercel.
|
|
'_vercel',
|
|
'assets',
|
|
// Top-level entries of `public/` (kept in sync by a unit test).
|
|
'archive',
|
|
'deno',
|
|
'favicon',
|
|
'fonts',
|
|
'img',
|
|
'json',
|
|
'monaco-editor',
|
|
'supabase-logo.svg',
|
|
]
|
|
|
|
const escapeRegex = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
|
|
// The two maintenance-mode rules are mutually exclusive; pick by env at
|
|
// build time. The lookahead matches whole segments only, so
|
|
// `/maintenance-foo` or `/images` still redirect.
|
|
export function getMaintenanceRedirects(maintenanceMode: boolean): StudioRedirect[] {
|
|
const passthrough = MAINTENANCE_PASSTHROUGH_SEGMENTS.map(escapeRegex).join('|')
|
|
return maintenanceMode
|
|
? [
|
|
{
|
|
source: `/((?!(?:${passthrough})(?:/|$)).*)`,
|
|
destination: '/maintenance',
|
|
permanent: false,
|
|
},
|
|
]
|
|
: [{ source: '/maintenance', destination: '/', permanent: false }]
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Runtime matcher — used by TanStack's `__root` beforeLoad so self-hosted
|
|
// (and any request Vercel's edge didn't already intercept) gets the same
|
|
// redirect behavior as Vercel's edge redirects.
|
|
//
|
|
// The patterns above are written in Vercel redirect syntax: `:name` for a
|
|
// single segment, `:name*` for a trailing catch-all, literals otherwise.
|
|
// That's all the redirect rules use, so a hand-rolled matcher is simpler
|
|
// (and dependency-free) than pulling in path-to-regexp here.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type RedirectMatch = { destination: string; permanent: boolean }
|
|
|
|
function matchPattern(pattern: string, pathname: string): Record<string, string> | null {
|
|
const patternParts = pattern.split('/')
|
|
const pathParts = pathname.split('/')
|
|
const params: Record<string, string> = {}
|
|
|
|
for (let i = 0; i < patternParts.length; i++) {
|
|
const p = patternParts[i]
|
|
if (p?.startsWith(':') && p.endsWith('*')) {
|
|
params[p.slice(1, -1)] = pathParts.slice(i).join('/')
|
|
return params
|
|
}
|
|
if (p?.startsWith(':')) {
|
|
const v = pathParts[i]
|
|
if (!v) return null
|
|
params[p.slice(1)] = decodeURIComponent(v)
|
|
continue
|
|
}
|
|
if (p !== pathParts[i]) return null
|
|
}
|
|
return patternParts.length === pathParts.length ? params : null
|
|
}
|
|
|
|
function substituteDestination(dest: string, params: Record<string, string>): string {
|
|
return dest.replace(/:(\w+)\*?/g, (_, name) => params[name] ?? '')
|
|
}
|
|
|
|
function hasQueryMatches(
|
|
has: StudioRedirect['has'],
|
|
search: URLSearchParams | Record<string, string | string[] | undefined>
|
|
): boolean {
|
|
if (!has?.length) return true
|
|
const get = (k: string) =>
|
|
search instanceof URLSearchParams
|
|
? search.get(k)
|
|
: Array.isArray(search[k])
|
|
? (search[k] as string[])[0]
|
|
: (search[k] as string | undefined)
|
|
return has.every((h) => h.type === 'query' && get(h.key) === h.value)
|
|
}
|
|
|
|
// Merge the incoming request's query params (and hash) onto a redirect
|
|
// destination, matching Vercel redirect semantics: the query string is
|
|
// carried through to the destination; params the destination already sets
|
|
// win; params consumed by the matched rule (its `has` query keys) are
|
|
// dropped; a hash on the destination wins over the incoming one. `hash` is
|
|
// the bare fragment without the leading `#`. Runtime-agnostic (also usable
|
|
// from the Vercel edge). Exported for unit tests.
|
|
export function preserveQueryAndHash(
|
|
destination: string,
|
|
search: URLSearchParams | Record<string, string | string[] | undefined>,
|
|
options: { consumedKeys?: string[]; hash?: string } = {}
|
|
): string {
|
|
const { consumedKeys = [], hash = '' } = options
|
|
|
|
const hashIdx = destination.indexOf('#')
|
|
const destHash = hashIdx >= 0 ? destination.slice(hashIdx + 1) : ''
|
|
const beforeHash = hashIdx >= 0 ? destination.slice(0, hashIdx) : destination
|
|
const queryIdx = beforeHash.indexOf('?')
|
|
const destPath = queryIdx >= 0 ? beforeHash.slice(0, queryIdx) : beforeHash
|
|
const params = new URLSearchParams(queryIdx >= 0 ? beforeHash.slice(queryIdx + 1) : '')
|
|
|
|
const incoming =
|
|
search instanceof URLSearchParams
|
|
? search
|
|
: new URLSearchParams(
|
|
Object.entries(search).flatMap(([key, value]): Array<[string, string]> => {
|
|
if (value === undefined || value === null) return []
|
|
if (Array.isArray(value)) return value.map((v) => [key, v])
|
|
return [[key, value]]
|
|
})
|
|
)
|
|
for (const key of new Set(incoming.keys())) {
|
|
if (consumedKeys.includes(key)) continue
|
|
if (params.has(key)) continue // destination's own value wins
|
|
for (const value of incoming.getAll(key)) params.append(key, value)
|
|
}
|
|
|
|
const queryStr = params.toString()
|
|
const finalHash = destHash || hash
|
|
return `${destPath}${queryStr ? `?${queryStr}` : ''}${finalHash ? `#${finalHash}` : ''}`
|
|
}
|
|
|
|
export function matchRedirect(input: {
|
|
pathname: string
|
|
search: URLSearchParams | Record<string, string | string[] | undefined>
|
|
isPlatform: boolean
|
|
maintenanceMode?: boolean
|
|
// Bare fragment without the leading `#` (TanStack's `location.hash` shape).
|
|
hash?: string
|
|
}): RedirectMatch | null {
|
|
const { pathname, search, isPlatform, maintenanceMode = false, hash } = input
|
|
|
|
// Maintenance mode handled inline — the maintenance-on rule uses a
|
|
// negative-lookahead regex source that the segment matcher above can't
|
|
// parse. Checks the same segment list the regex is built from.
|
|
if (maintenanceMode) {
|
|
const firstSegment = pathname.split('/')[1] ?? ''
|
|
if (!MAINTENANCE_PASSTHROUGH_SEGMENTS.includes(firstSegment)) {
|
|
return {
|
|
destination: preserveQueryAndHash('/maintenance', search, { hash }),
|
|
permanent: false,
|
|
}
|
|
}
|
|
} else if (pathname === '/maintenance') {
|
|
return { destination: preserveQueryAndHash('/', search, { hash }), permanent: false }
|
|
}
|
|
|
|
const ordered = [
|
|
...(isPlatform ? PLATFORM_REDIRECTS : SELF_HOSTED_REDIRECTS),
|
|
...SHARED_REDIRECTS,
|
|
]
|
|
for (const rule of ordered) {
|
|
const params = matchPattern(rule.source, pathname)
|
|
if (!params) continue
|
|
if (!hasQueryMatches(rule.has, search)) continue
|
|
return {
|
|
// Carry the incoming query and hash through to the destination;
|
|
// keys the rule matched on (`has`) are consumed.
|
|
destination: preserveQueryAndHash(substituteDestination(rule.destination, params), search, {
|
|
consumedKeys: rule.has?.map((h) => h.key) ?? [],
|
|
hash,
|
|
}),
|
|
permanent: rule.permanent,
|
|
}
|
|
}
|
|
return null
|
|
}
|