mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Cleans up obsolete Rolldown/Vite bundler workarounds from the TanStack Start migration and fixes unstable router selectors and inline default array props that could cause unnecessary re-renders or render loops. Addresses [FE-3200](https://linear.app/supabase/issue/FE-3200/lift-bundler-workarounds-and-fix-latent-issues-next-was-masking). **Removed:** - Emptied `KNOWN_CHUNK_CYCLES` in `apps/studio/vite.config.ts` while keeping the `assertNoChunkCycles` build guard enabled - Removed obsolete `manualChunks` workarounds (`class-variance-authority`, `ui-field`, `ui-drawer`, `ui-form`, `ui-sidebar`, `ui-slider`, `ui-avatar`), keeping the React and `lucide-react` vendor chunks so React stays a single leaf chunk and Lucide does not fan out into per-icon modulepreload requests - Removed the obsolete `sharedDefines` (`global: 'globalThis'`) substitution from `apps/studio/vite.config.ts` **Changed:** - `StudioLink` (`StudioFrameworkProvider.tsx`): split `useRouterState` into primitive `currentPathname` and `currentSearch` selectors instead of returning a fresh object on every router state tick - `useNuqsTanStackRouterAdapter` (`nuqs-tanstack-adapter.tsx`): select a serialized query string key from `useLocation` so `useQueryState` consumers do not re-render when unwatched search params change - `FilterPopover`: replaced inline `[]` defaults for `options` and `activeOptions` with shared `EMPTY_ARR` (`@/lib/void`), and guarded `setSelectedOptions` with an element-wise equality check when closed - `InputWithSuggestions`: replaced inline `[]` default for `suggestions` with shared `EMPTY_ARR` (`@/lib/void`) so `useEffect` does not loop when `suggestions` is omitted at runtime **Added:** - Regression tests covering router selector stability and default-prop render loops in `StudioFrameworkProvider.test.tsx`, `nuqs-tanstack-adapter.test.tsx`, `FilterPopover.test.tsx`, and `InputWithSuggestions.test.tsx` ## To test - Run `pnpm --filter studio run build` and verify the build passes with `KNOWN_CHUNK_CYCLES` empty and `assertNoChunkCycles` enabled - Run the new/updated unit tests in `apps/studio` (`StudioFrameworkProvider.test.tsx`, `nuqs-tanstack-adapter.test.tsx`, `FilterPopover.test.tsx`, `InputWithSuggestions.test.tsx`) - Smoke-test Studio navigation, URL search-param filters (`nuqs`), Table Editor column suggestions, `FilterPopover` interactions, and `/auth/hooks` (confirming no `global` ReferenceError) --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
90 lines
3.8 KiB
TypeScript
90 lines
3.8 KiB
TypeScript
import { useLocation, useNavigate, useRouter, type AnyRouter } from '@tanstack/react-router'
|
|
import { unstable_createAdapterProvider } from 'nuqs/adapters/custom'
|
|
import { startTransition, useCallback, useMemo } from 'react'
|
|
|
|
import { searchParamsToRecord, type SearchRecord } from './router-search-params'
|
|
|
|
// Custom nuqs adapter for TanStack Router.
|
|
//
|
|
// The stock `nuqs/adapters/tanstack-router` adapter navigates with
|
|
// `navigate({ to: renderQueryString(search) || '.', from })`. That shape is
|
|
// doubly broken for us:
|
|
// - TanStack resolves a `?`-only relative `to` by *appending* it to the
|
|
// current path, injecting a trailing slash — every nuqs write turns
|
|
// `/auth/providers?provider=x` into `/auth/providers/?provider=x`.
|
|
// - Embedding the query string in `to` at all sends it through TanStack's
|
|
// path interpolation, which percent-decodes and then strips control
|
|
// characters — `%0A` newlines in values (e.g. Logs Explorer SQL in `s`)
|
|
// are silently deleted.
|
|
// So we navigate with `to` = the current pathname and the query as a
|
|
// TanStack `search` *object*, which the router serialises through the app's
|
|
// Next-style codec (lib/router-search-params) without touching the path
|
|
// pipeline. Everything else (searchParams derivation, replace/push, scroll,
|
|
// hash preservation) mirrors the stock adapter's contract.
|
|
|
|
type AdapterOptions = { history: 'push' | 'replace'; scroll: boolean; shallow: boolean }
|
|
|
|
// Compose the nuqs-updated URLSearchParams into TanStack navigate args:
|
|
// `to` is the current pathname (from TanStack's parsed location, already
|
|
// basepath-stripped; guard against a trailing slash anyway — root stays `/`)
|
|
// and `search` is the FULL desired search state as a record ({} correctly
|
|
// clears every param). Exported for unit tests — not part of the adapter
|
|
// surface.
|
|
export function buildSearchUpdateArgs(
|
|
pathname: string,
|
|
search: URLSearchParams
|
|
): { to: string; search: SearchRecord } {
|
|
let to = pathname || '/'
|
|
if (to.length > 1 && to.endsWith('/')) to = to.slice(0, -1)
|
|
return { to, search: searchParamsToRecord(search) }
|
|
}
|
|
|
|
function useNuqsTanStackRouterAdapter(watchKeys: string[]) {
|
|
const searchKey = useLocation({
|
|
select: (state) =>
|
|
new URLSearchParams(
|
|
Object.entries(state.search)
|
|
.filter(([key]) => watchKeys.includes(key))
|
|
.flatMap(([key, value]): Array<[string, string]> => {
|
|
if (Array.isArray(value)) return value.map((v) => [key, String(v)])
|
|
if (typeof value === 'object' && value !== null) return [[key, JSON.stringify(value)]]
|
|
return [[key, String(value)]]
|
|
})
|
|
).toString(),
|
|
})
|
|
const router = useRouter()
|
|
const navigate = useNavigate()
|
|
|
|
const searchParams = useMemo(() => new URLSearchParams(searchKey), [searchKey])
|
|
|
|
const updateUrl = useCallback(
|
|
(search: URLSearchParams, options: AdapterOptions) => {
|
|
// Read the location at call time: nuqs setters can outlive the render that created them.
|
|
const args = buildSearchUpdateArgs(router.state.location.pathname, search)
|
|
startTransition(() => {
|
|
// The `<AnyRouter, string>` type arguments opt out of the registered
|
|
// route tree's strict typing: the current pathname is a free-form
|
|
// runtime string that can't satisfy the route-path union at compile
|
|
// time.
|
|
navigate<AnyRouter, string>({
|
|
to: args.to,
|
|
search: args.search,
|
|
replace: options.history === 'replace',
|
|
resetScroll: options.scroll,
|
|
// Keep the current hash — nuqs updates must not clear `#section`.
|
|
hash: (prevHash) => prevHash ?? '',
|
|
})
|
|
})
|
|
},
|
|
[navigate, router]
|
|
)
|
|
|
|
return {
|
|
searchParams,
|
|
updateUrl,
|
|
rateLimitFactor: 1,
|
|
}
|
|
}
|
|
|
|
export const NuqsAdapter = unstable_createAdapterProvider(useNuqsTanStackRouterAdapter)
|