mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
Introduces a lightweight `FrameworkProvider` React context in `packages/common` (`Link`, `Image`, `pathname`, `asPath`, `isReady`, `params`, `push`), wires it up in the consuming apps, and removes the `next` dependency from `packages/common`. **Added:** - `FrameworkProvider`, `useFramework`, `FrameworkLink`, and `FrameworkImage` in `packages/common/framework.tsx`, with plain HTML defaults (`DefaultFrameworkLink` / `DefaultFrameworkImage`). `useFramework().push` always exists and falls back to `window.location.assign` - App-level framework providers wiring `next/link`, `next/image`, and router state in `www` (App + Pages routers), `docs`, `design-system`, `learn`, and `ui-library`, and native TanStack Router bindings in `studio` (`StudioFrameworkProvider`) - `StudioFrameworkProvider` reuses the common defaults (its image only applies the basePath) and reports `isReady` once the first route load commits, so the first page view carries the final route after any redirect - Unit tests for `StudioFrameworkProvider` (`useParams`, `FrameworkLink`, `FrameworkImage`, query/hash-only navigation, `isReady`) and for `PageTelemetry` route-change sends and the `TelemetryTagManager` consent gate **Changed:** - `common/hooks/useParams.ts`: read params from `useFramework()` instead of `next/compat/router` - `common/telemetry.tsx`: replace `next/script` in `TelemetryTagManager` with `useEffect` script injection, and read route state from `useFramework()` in `PageTelemetry` - `common/MetaFavicons/app-router.ts`: drop type-only `Metadata` import from `next` - Moved `MetaFaviconsPagesRouter` to `apps/www/components/MetaFaviconsPagesRouter.tsx` (its sole consumer) - Studio: `toRoutePattern` and `applyBasePath` now live once in `lib/internal-url.ts`, shared by the provider and the `compat/next` shims - `PageTelemetry` throws outside production when no `FrameworkProvider` is mounted, since it would otherwise send only the landing pageview. `useFramework()` itself still falls back to the defaults - `ui-library`: renamed its code-sample framework context to `FrameworkPreferenceProvider` / `useFrameworkPreference` so `FrameworkProvider` only means the `common` one **Removed:** - `learn`'s unused copy of that context (`useCourse` had no callers) - `next` from `packages/common/package.json` peerDependencies App Router adapters set `asPath` to the pathname only, same as the `usePathname`-based telemetry on master (reading search params at the root would opt static pages out of static rendering). ## To test - Run `packages/common` tests and typecheck, and `apps/studio` `StudioFrameworkProvider` / `route-render` / `telemetry` tests - Navigate between routes in Studio, Docs, and WWW and confirm page telemetry fires once per navigation (and does not duplicate on hash-only changes) - Load Studio at `/` (which redirects) and confirm the first page event has the final route - Verify URL params (`useParams`) continue resolving route and search params in Studio --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
119 lines
4.1 KiB
TypeScript
119 lines
4.1 KiB
TypeScript
import {
|
|
createMemoryHistory,
|
|
createRootRoute,
|
|
createRoute,
|
|
createRouter,
|
|
RouterProvider,
|
|
} from '@tanstack/react-router'
|
|
import { act, render, screen, waitFor } from '@testing-library/react'
|
|
import { FrameworkImage, FrameworkLink, useFramework, useParams } from 'common'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import { StudioFrameworkProvider } from './StudioFrameworkProvider'
|
|
import { renderRoute } from '@/tests/lib/route-render'
|
|
|
|
function ProbeComponent() {
|
|
const params = useParams()
|
|
const { pathname, asPath, push } = useFramework()
|
|
|
|
return (
|
|
<div>
|
|
<span data-testid="pathname">{pathname}</span>
|
|
<span data-testid="asPath">{asPath}</span>
|
|
<span data-testid="ref">{params.ref}</span>
|
|
<span data-testid="sortOrder">{params.sortOrder}</span>
|
|
<button type="button" tabIndex={0} onClick={() => push('?sort-order=desc')}>
|
|
Update query
|
|
</button>
|
|
<FrameworkLink href="/project/abc/editor?schema=public">Internal</FrameworkLink>
|
|
<FrameworkLink href="https://supabase.com/docs">External</FrameworkLink>
|
|
<FrameworkImage src="/img/example.png" alt="Example" fill priority />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const ProbeRoute = createRoute({
|
|
getParentRoute: () => createRootRoute(),
|
|
path: 'project/$ref/editor',
|
|
component: ProbeComponent,
|
|
})
|
|
|
|
describe('StudioFrameworkProvider', () => {
|
|
it('provides TanStack route pattern, asPath, params, Link, and Image through common FrameworkContext', async () => {
|
|
await renderRoute(ProbeRoute, {
|
|
routeId: '/project/$ref/editor',
|
|
url: '/project/my-proj/editor?sort-order=asc&ref=ignored',
|
|
})
|
|
|
|
expect(screen.getByTestId('pathname')).toHaveTextContent('/project/[ref]/editor')
|
|
expect(screen.getByTestId('asPath')).toHaveTextContent(
|
|
'/project/my-proj/editor?sort-order=asc&ref=ignored'
|
|
)
|
|
// Route params take precedence over search params and kebab-case keys are camelCased
|
|
expect(screen.getByTestId('ref')).toHaveTextContent('my-proj')
|
|
expect(screen.getByTestId('sortOrder')).toHaveTextContent('asc')
|
|
|
|
expect(screen.getByRole('link', { name: 'Internal' })).toHaveAttribute(
|
|
'href',
|
|
'/project/abc/editor?schema=public'
|
|
)
|
|
expect(screen.getByRole('link', { name: 'External' })).toHaveAttribute(
|
|
'href',
|
|
'https://supabase.com/docs'
|
|
)
|
|
|
|
const img = screen.getByAltText('Example')
|
|
expect(img).toHaveAttribute('src', '/img/example.png')
|
|
expect(img).toHaveAttribute('loading', 'eager')
|
|
expect(img).toHaveStyle({ position: 'absolute' })
|
|
|
|
// Query-only push stays on the current route instead of navigating to '/'
|
|
await act(async () => {
|
|
screen.getByRole('button', { name: 'Update query' }).click()
|
|
})
|
|
expect(screen.getByTestId('pathname')).toHaveTextContent('/project/[ref]/editor')
|
|
expect(screen.getByTestId('sortOrder')).toHaveTextContent('desc')
|
|
})
|
|
|
|
it('reports isReady only once the initial route load has committed', async () => {
|
|
let finishLoading!: () => void
|
|
const loading = new Promise<void>((resolve) => {
|
|
finishLoading = resolve
|
|
})
|
|
|
|
function ReadyProbe() {
|
|
const { isReady } = useFramework()
|
|
return <span data-testid="isReady">{String(isReady)}</span>
|
|
}
|
|
|
|
const rootRoute = createRootRoute({
|
|
shellComponent: ({ children }) => (
|
|
<StudioFrameworkProvider>
|
|
<ReadyProbe />
|
|
{children}
|
|
</StudioFrameworkProvider>
|
|
),
|
|
})
|
|
const editorRoute = createRoute({
|
|
getParentRoute: () => rootRoute,
|
|
path: 'project/$ref/editor',
|
|
beforeLoad: () => loading,
|
|
component: () => null,
|
|
})
|
|
|
|
const router = createRouter({
|
|
routeTree: rootRoute.addChildren([editorRoute]),
|
|
history: createMemoryHistory({ initialEntries: ['/project/my-proj/editor'] }),
|
|
defaultPendingMs: 0,
|
|
defaultPendingMinMs: 0,
|
|
defaultPendingComponent: () => null,
|
|
})
|
|
|
|
render(<RouterProvider router={router} />)
|
|
expect(await screen.findByTestId('isReady')).toHaveTextContent('false')
|
|
|
|
finishLoading()
|
|
await waitFor(() => expect(screen.getByTestId('isReady')).toHaveTextContent('true'))
|
|
})
|
|
})
|