mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 07:59:03 +00:00
Part 6 (final) of a 6-PR stack migrating all `next/link` imports in `apps/studio` onto TanStack Router's `Link` (or plain `<a>` for known external URLs) and removing `compat/next/link.tsx`. | Part | PR | Scope | | --- | --- | --- | | 1 | #54304 | `data/prefetchers/PrefetchableLink.tsx` and `components/ui/**` | | 2 | #54305 | `components/layouts/**` and top-level shell components | | 3 | #54306 | Database, TableGridEditor, Storage, SQLEditor, BranchManagement, Branching, Functions, Realtime | | 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues, Linter, Reports, QueryPerformance, QuerySources, ConfigDrift, DiskManagement, Compute | | 5 | #54308 | Remaining `components/interfaces/**` (Org, Billing, Auth, Support, Home, SignIn, ConnectSheet, etc.) | | **6** | **#54309 ← this PR** | **Remove `compat/next/link.tsx` shim, Vite alias, and update basePath tests/docs** | Now that no files in `apps/studio` import `next/link`, deletes `compat/next/link.tsx` and removes its Vite alias so any future `next/link` import fails the build. **Removed:** - `apps/studio/compat/next/link.tsx` - `'next/link'` entry from `nextShims` in `apps/studio/vite.config.ts` **Changed:** - Moved `apps/studio/compat/next/link-basepath-navigation.test.tsx` to `apps/studio/lib/navigation-basepath.test.tsx` to keep basePath navigation coverage for `DynamicLink` and `navigateToUrl` - Updated `nextCompat()` error text in `apps/studio/vite.config.ts` and routing docs in `apps/studio/AGENTS.md` to reflect that only `next/script` remains shimmed - Cleaned up stale `next/link` / `Link shim` comments in `apps/studio/lib/internal-url.ts`, `apps/studio/lib/router-url-shape.test.tsx`, and `knip.jsonc` Addresses [FE-5108](https://linear.app/supabase/issue/FE-5108/replace-the-nextlink-shim-with-tanstack-link) ## To test - [ ] Confirm `git grep -E 'next/link|compat/next/link' apps/studio knip.jsonc` returns zero matches - [ ] Confirm Studio typecheck, ESLint ratchet, Knip, production Vite build, and Vitest suite pass - [ ] Spot-check `apps/studio/lib/navigation-basepath.test.tsx` and `apps/studio/lib/router-url-shape.test.tsx` Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
164 lines
5.4 KiB
TypeScript
164 lines
5.4 KiB
TypeScript
import {
|
|
createMemoryHistory,
|
|
createRootRoute,
|
|
createRoute,
|
|
createRouter,
|
|
Outlet,
|
|
RouterProvider,
|
|
useNavigate,
|
|
type UseNavigateResult,
|
|
} from '@tanstack/react-router'
|
|
import { act, fireEvent, render, screen } from '@testing-library/react'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { DynamicLink } from '@/components/ui/DynamicLink'
|
|
import { navigateToUrl } from '@/lib/navigation'
|
|
import { parseSearch, stringifySearch } from '@/lib/router-search-params'
|
|
|
|
// `internal-url` reads NEXT_PUBLIC_BASE_PATH at module load, so it has to be
|
|
// set before the static imports above are evaluated.
|
|
vi.hoisted(() => {
|
|
process.env.NEXT_PUBLIC_BASE_PATH = '/dashboard'
|
|
})
|
|
|
|
async function renderWithRouter(
|
|
ui: (nav: { current?: UseNavigateResult<string> }) => React.ReactNode
|
|
) {
|
|
const nav: { current?: UseNavigateResult<string> } = {}
|
|
function Page() {
|
|
nav.current = useNavigate()
|
|
return <>{ui(nav)}</>
|
|
}
|
|
const rootRoute = createRootRoute({ component: Outlet })
|
|
const page = createRoute({
|
|
getParentRoute: () => rootRoute,
|
|
path: '/project/$ref',
|
|
component: Page,
|
|
})
|
|
const router = createRouter({
|
|
routeTree: rootRoute.addChildren([page]),
|
|
basepath: '/dashboard',
|
|
history: createMemoryHistory({ initialEntries: ['/dashboard/project/default'] }),
|
|
parseSearch,
|
|
stringifySearch,
|
|
isServer: false,
|
|
})
|
|
await router.load()
|
|
const view = render(<RouterProvider router={router} />)
|
|
return { router, nav, view }
|
|
}
|
|
|
|
const origin = window.location.origin
|
|
|
|
describe('DynamicLink + navigateToUrl with a configured basePath', () => {
|
|
it('renders a same-origin URL outside the basePath as a plain anchor to the full URL', async () => {
|
|
const href = `${origin}/docs/guides/platform/manage-your-usage/compute`
|
|
const { router, view } = await renderWithRouter(() => (
|
|
<DynamicLink href={href}>Compute Costs</DynamicLink>
|
|
))
|
|
try {
|
|
const link = screen.getByRole('link', { name: 'Compute Costs' })
|
|
expect(link).toHaveAttribute('href', href)
|
|
const navigate = vi.spyOn(router, 'navigate')
|
|
fireEvent.click(link)
|
|
expect(navigate).not.toHaveBeenCalled()
|
|
expect(router.state.location.href).toBe('/project/default')
|
|
} finally {
|
|
view.unmount()
|
|
}
|
|
})
|
|
|
|
it('keeps basepath-relative and basePath-prefixed links internal', async () => {
|
|
const { view } = await renderWithRouter(() => (
|
|
<>
|
|
<DynamicLink href="/project/abc">relative</DynamicLink>
|
|
<DynamicLink href="/dashboard/project/abc">prefixed</DynamicLink>
|
|
<DynamicLink href={`${origin}/dashboard/project/abc`}>absolute</DynamicLink>
|
|
</>
|
|
))
|
|
try {
|
|
for (const name of ['relative', 'prefixed', 'absolute']) {
|
|
expect(screen.getByText(name).closest('a')).toHaveAttribute(
|
|
'href',
|
|
'/dashboard/project/abc'
|
|
)
|
|
}
|
|
} finally {
|
|
view.unmount()
|
|
}
|
|
})
|
|
|
|
it.each([
|
|
['same-origin outside the basePath', `${origin}/docs/guides`],
|
|
['cross-origin', 'https://example.com/docs#install'],
|
|
])(
|
|
'navigateToUrl() of a %s URL does a document navigation to the original URL',
|
|
async (_, url) => {
|
|
const { router, nav, view } = await renderWithRouter(() => null)
|
|
const navigate = vi.spyOn(router, 'navigate').mockResolvedValue(undefined)
|
|
try {
|
|
await act(async () => {
|
|
await navigateToUrl(nav.current!, url)
|
|
})
|
|
expect(navigate).toHaveBeenCalledWith({ href: url, reloadDocument: true })
|
|
await act(async () => {
|
|
await navigateToUrl(nav.current!, url, { replace: true })
|
|
})
|
|
expect(navigate).toHaveBeenLastCalledWith({
|
|
href: url,
|
|
reloadDocument: true,
|
|
replace: true,
|
|
})
|
|
} finally {
|
|
view.unmount()
|
|
}
|
|
}
|
|
)
|
|
|
|
it('navigateToUrl() of an external URL leaves the client-side location alone', async () => {
|
|
// Unmocked: TanStack hands it to `window.location` (a no-op in jsdom)
|
|
// instead of resolving it as a path segment (`/project/default/https:/...`).
|
|
const { router, nav, view } = await renderWithRouter(() => null)
|
|
try {
|
|
await act(async () => {
|
|
await navigateToUrl(nav.current!, `${origin}/docs/guides`)
|
|
})
|
|
expect(router.state.location.href).toBe('/project/default')
|
|
} finally {
|
|
view.unmount()
|
|
}
|
|
})
|
|
|
|
it('navigateToUrl() of an internal path stays a client-side navigation', async () => {
|
|
const { router, nav, view } = await renderWithRouter(() => null)
|
|
const navigate = vi.spyOn(router, 'navigate')
|
|
try {
|
|
await act(async () => {
|
|
await navigateToUrl(nav.current!, `${origin}/dashboard/project/other?tab=1`)
|
|
})
|
|
expect(navigate).toHaveBeenCalledWith(
|
|
expect.objectContaining({ to: '/project/other', search: { tab: '1' } })
|
|
)
|
|
expect(router.state.location.pathname).toBe('/project/other')
|
|
} finally {
|
|
view.unmount()
|
|
}
|
|
})
|
|
|
|
it('navigateToUrl() resolves a protocol-relative URL so TanStack does not block it', async () => {
|
|
const { router, nav, view } = await renderWithRouter(() => null)
|
|
const navigate = vi.spyOn(router, 'navigate').mockResolvedValue(undefined)
|
|
try {
|
|
await act(async () => {
|
|
await navigateToUrl(nav.current!, '//example.com/docs')
|
|
})
|
|
expect(navigate).toHaveBeenCalledWith({
|
|
href: `${window.location.protocol}//example.com/docs`,
|
|
reloadDocument: true,
|
|
})
|
|
} finally {
|
|
view.unmount()
|
|
}
|
|
})
|
|
})
|