mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
Part 6 (final) of a 6-PR stack migrating all `next/router` and `next/compat/router` imports in `apps/studio` onto TanStack Router hooks and removing the router shims. | Part | PR | Scope | | --- | --- | --- | | 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`, `data/prefetchers/**` | | 2 | #54054 | `components/ui/**` | | 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` | | 4 | #54056 | Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | **6** | **#54058 ← this PR** | **Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks** | Now that no files in `apps/studio` (or `packages/**`) import `next/router` or `next/compat/router`, deletes the router shims, `_router-events.ts`, their Vite aliases, and the obsolete router test mocks. `RouteValidationWrapper` is intentionally left for FE-3107. **Removed:** - `apps/studio/compat/next/router.ts`, `apps/studio/compat/next/compat/router.ts`, and `apps/studio/compat/next/_router-events.ts` - `'next/router'` and `'next/compat/router'` entries from `nextShims` in `apps/studio/vite.config.ts` - Global `vi.mock('next/router', ...)` and `vi.mock('next/compat/router', ...)` in `apps/studio/tests/vitestSetup.ts`, `mockNextRouter` in `apps/studio/tests/lib/route-aware-hooks.ts`, and the unused `vi.mock('next/router')` in `tests/features/logs/LogTable.test.tsx` - Obsolete shim tests and test helpers: `compat/next/router.test.ts`, `compat/next/router-navigation.test.tsx`, `tests/config/router.tsx`, and `tests/config/router.test.tsx` **Changed:** - Simplified `createNavigationHandler` in `apps/studio/lib/navigation.ts` (and `navigation.test.ts`) to accept TanStack's `UseNavigateResult<string>` only, dropping the `NextRouter` union branch - Updated `compat/next/link-basepath-navigation.test.tsx` (renamed from `router-basepath-navigation.test.tsx`), `lib/router-url-shape.test.tsx`, and `tests/lib/route-render.test.tsx` to exercise TanStack Router hooks directly instead of the removed router shim - Cleaned up stale `next/router` comments in `compat/next/link.tsx`, `lib/internal-url.ts`, `router.tsx`, and `tests/lib/route-mock.ts` **Simplified:** - Removed `routerMock` setup from about 19 tests that no longer read it. `next-router-mock` stays for now, because the test `FrameworkProvider` still feeds `useParams` from it. - `AGENTS.md` and the `vite.config.ts` error text now say only `next/link` and `next/script` are shimmed - Renamed `compat/next/router-basepath-navigation.test.tsx` to `link-basepath-navigation.test.tsx`, and removed stale router-shim references from comments Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] Confirm `git grep -E "next/(compat/)?router|_router-events" apps/studio` returns no code or config imports (only the prose guideline in `apps/studio/AGENTS.md`) - [ ] Confirm `pnpm --filter studio run typecheck`, `pnpm --filter studio run lint`, `pnpm --filter studio run build`, Knip, and the full Studio Vitest suite pass - [ ] Spot-check basePath navigation (`next/link` + `navigateToUrl`) and multi-line query param URLs in Studio --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
192 lines
7.0 KiB
TypeScript
192 lines
7.0 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { isAbsoluteUrl, splitInternalUrl, toRoutePattern } from './internal-url'
|
|
|
|
describe('splitInternalUrl', () => {
|
|
it('splits pathname, search, and hash', () => {
|
|
expect(splitInternalUrl('/project/abc/editor/123?schema=public#section')).toEqual({
|
|
to: '/project/abc/editor/123',
|
|
search: { schema: 'public' },
|
|
hash: 'section',
|
|
})
|
|
})
|
|
|
|
it('strips the leading "#" from the hash so TanStack does not produce "##section"', () => {
|
|
expect(splitInternalUrl('/org/slug/billing#invoices')).toEqual({
|
|
to: '/org/slug/billing',
|
|
search: undefined,
|
|
hash: 'invoices',
|
|
})
|
|
})
|
|
|
|
it('omits search and hash when absent', () => {
|
|
expect(splitInternalUrl('/projects')).toEqual({
|
|
to: '/projects',
|
|
search: undefined,
|
|
hash: undefined,
|
|
})
|
|
})
|
|
|
|
it('preserves repeated query keys as arrays', () => {
|
|
expect(splitInternalUrl('/advisors/security?filter=a:eq:1&filter=b:eq:2&page=2')).toEqual({
|
|
to: '/advisors/security',
|
|
search: { filter: ['a:eq:1', 'b:eq:2'], page: '2' },
|
|
hash: undefined,
|
|
})
|
|
})
|
|
|
|
it('keeps query values as plain strings (no JSON coercion)', () => {
|
|
expect(splitInternalUrl('/x?flag=true&page=2').search).toEqual({ flag: 'true', page: '2' })
|
|
})
|
|
|
|
it('decodes %0A (and other control chars) in query values losslessly', () => {
|
|
// The regression this split exists to prevent: query text embedded in a
|
|
// TanStack `to` goes through path interpolation, which strips control
|
|
// characters after percent-decoding. Splitting keeps the raw value.
|
|
const sql = 'select *\nfrom logs\norder by timestamp desc\nlimit 5'
|
|
const { to, search } = splitInternalUrl(
|
|
`/project/abc/logs/explorer?s=${encodeURIComponent(sql)}`
|
|
)
|
|
expect(to).toBe('/project/abc/logs/explorer')
|
|
expect(search).toEqual({ s: sql })
|
|
})
|
|
|
|
it('normalizes same-origin absolute URLs to a relative path', () => {
|
|
const url = new URL('/project/abc/editor/1?schema=public#top', window.location.origin)
|
|
expect(splitInternalUrl(url.toString())).toEqual({
|
|
to: '/project/abc/editor/1',
|
|
search: { schema: 'public' },
|
|
hash: 'top',
|
|
})
|
|
})
|
|
|
|
it('normalizes any same-origin absolute URL when no basePath is configured', () => {
|
|
expect(splitInternalUrl(`${window.location.origin}/docs/guides`)).toEqual({
|
|
to: '/docs/guides',
|
|
search: undefined,
|
|
hash: undefined,
|
|
})
|
|
})
|
|
|
|
it('leaves cross-origin and protocol-relative URLs untouched', () => {
|
|
expect(splitInternalUrl('https://supabase.com/docs#install')).toEqual({
|
|
to: 'https://supabase.com/docs#install',
|
|
})
|
|
expect(splitInternalUrl('//supabase.com/docs')).toEqual({ to: '//supabase.com/docs' })
|
|
})
|
|
})
|
|
|
|
describe('splitInternalUrl under SSR (no window)', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it('leaves cross-origin absolute URLs untouched', () => {
|
|
vi.stubGlobal('window', undefined)
|
|
expect(splitInternalUrl('https://supabase.com/docs')).toEqual({
|
|
to: 'https://supabase.com/docs',
|
|
})
|
|
})
|
|
|
|
it('still splits relative paths', () => {
|
|
vi.stubGlobal('window', undefined)
|
|
expect(splitInternalUrl('/project/abc/editor/123?schema=public#section')).toEqual({
|
|
to: '/project/abc/editor/123',
|
|
search: { schema: 'public' },
|
|
hash: 'section',
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('splitInternalUrl with a configured basePath', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs()
|
|
vi.resetModules()
|
|
})
|
|
|
|
it('strips the basePath prefix so TanStack does not double-prefix', async () => {
|
|
vi.stubEnv('NEXT_PUBLIC_BASE_PATH', '/dashboard')
|
|
vi.resetModules()
|
|
const { splitInternalUrl: split } = await import('./internal-url')
|
|
expect(split('/dashboard/project/abc?schema=public#top')).toEqual({
|
|
to: '/project/abc',
|
|
search: { schema: 'public' },
|
|
hash: 'top',
|
|
})
|
|
expect(split('/dashboard')).toEqual({ to: '/', search: undefined, hash: undefined })
|
|
// A coincidental prefix is not stripped.
|
|
expect(split('/dashboard-other/x').to).toBe('/dashboard-other/x')
|
|
})
|
|
|
|
it('leaves same-origin absolute URLs outside the basePath untouched', async () => {
|
|
// Next only treats an absolute URL as local when it is same-origin AND
|
|
// under the basePath. `https://supabase.com/docs` from `/dashboard` must
|
|
// not become `to: '/docs'` (TanStack would prefix it to `/dashboard/docs`).
|
|
vi.stubEnv('NEXT_PUBLIC_BASE_PATH', '/dashboard')
|
|
vi.resetModules()
|
|
const { splitInternalUrl: split } = await import('./internal-url')
|
|
const docs = `${window.location.origin}/docs/guides/platform/manage-your-usage/compute#top`
|
|
expect(split(docs)).toEqual({ to: docs })
|
|
expect(split(`${window.location.origin}/dashboard-other/x`)).toEqual({
|
|
to: `${window.location.origin}/dashboard-other/x`,
|
|
})
|
|
})
|
|
|
|
it('still strips same-origin absolute URLs under the basePath', async () => {
|
|
vi.stubEnv('NEXT_PUBLIC_BASE_PATH', '/dashboard')
|
|
vi.resetModules()
|
|
const { splitInternalUrl: split } = await import('./internal-url')
|
|
expect(split(`${window.location.origin}/dashboard/project/abc?schema=public`)).toEqual({
|
|
to: '/project/abc',
|
|
search: { schema: 'public' },
|
|
hash: undefined,
|
|
})
|
|
expect(split(`${window.location.origin}/dashboard`).to).toBe('/')
|
|
})
|
|
|
|
it('keeps relative inputs outside the basePath internal', async () => {
|
|
// Relative hrefs are always local in Next; studio code writes them
|
|
// basepath-relative, so `/docs` here is a studio path, not the docs app.
|
|
vi.stubEnv('NEXT_PUBLIC_BASE_PATH', '/dashboard')
|
|
vi.resetModules()
|
|
const { splitInternalUrl: split } = await import('./internal-url')
|
|
expect(split('/docs/x').to).toBe('/docs/x')
|
|
})
|
|
|
|
it('passes already basepath-relative inputs through unchanged', async () => {
|
|
// Callers may pass a target that is already basepath-relative. Stripping
|
|
// again must be a no-op for every real studio route (none starts with the
|
|
// basePath segment).
|
|
vi.stubEnv('NEXT_PUBLIC_BASE_PATH', '/dashboard')
|
|
vi.resetModules()
|
|
const { splitInternalUrl: split } = await import('./internal-url')
|
|
expect(split('/project/abc/logs/explorer?s=select%201')).toEqual({
|
|
to: '/project/abc/logs/explorer',
|
|
search: { s: 'select 1' },
|
|
hash: undefined,
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('isAbsoluteUrl', () => {
|
|
it('detects URLs that carry their own origin', () => {
|
|
expect(isAbsoluteUrl('https://supabase.com/docs')).toBe(true)
|
|
expect(isAbsoluteUrl('mailto:support@supabase.com')).toBe(true)
|
|
expect(isAbsoluteUrl('//supabase.com/docs')).toBe(true)
|
|
expect(isAbsoluteUrl('/project/abc')).toBe(false)
|
|
expect(isAbsoluteUrl('?tab=1')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('toRoutePattern', () => {
|
|
it.each([
|
|
['/_app/project/$ref/sql/$id', '/project/[ref]/sql/[id]'],
|
|
['/_app/project/$ref/', '/project/[ref]'],
|
|
['/', '/'],
|
|
['/_app/project/$ref/functions/$functionSlug/$', '/project/[ref]/functions/[functionSlug]/$'],
|
|
])('maps %s to %s', (routeId, expected) => {
|
|
expect(toRoutePattern(routeId)).toBe(expected)
|
|
})
|
|
})
|