Files
supabase/apps/studio/router.tsx
Joshen Lim 999b6acb56 Add a global translation DOM guard which no ops google translate DOM errors (#54583)
## Context

Another attempt to address the Chrome browser translation DOM crashes
issues (`NotFoundError: Failed to execute 'removeChild' on 'Node'` and
`NotFoundError: Failed to execute 'insertBefore' on 'Node'`)

We previously tried to mitigate this problem by wrapping conditional
texts in spans here: https://github.com/supabase/supabase/pull/52949,
but even after so we're still seeing events for those errors in Sentry
although the numbers have dropped significantly. The main problem from
the user's POV is recovery from DOM crashes which they'll need to
refresh the browser - it's not just annoying but they could lose unsaved
SQL if the crash happened within the SQL Editor.

Attempting to fully mitigate this via React's
[recommendation](https://github.com/react/react/issues/11538#issuecomment-417504600)
to patch `Node.prototype.removeChild` and `insertBefore` which i'll
detail the changes below.

## Changes involved
- Added `lib/translation-dom-guard.ts`, installed once from
`getRouter()`, which It patches `Node.prototype.removeChild` and
`insertBefore` to basically no-op but ONLY when the page is translated
(so real bugs still throw)
- If the error happens on untranslated pages, they will still throw as
before
- The side effect here is just that the UI may show a stale label in
that spot, because React updated or removed a node that Translate had
already replaced, but imo that's alright and better than a full DOM
crash

We can subsequently then monitor Sentry again to see if these 2 errors
_still_ occur, but the main signal that this hopes to drive down is just
to minimize the number of affected users
2026-12-04 15:02:06 +07:00

60 lines
2.0 KiB
TypeScript

import type { QueryClient } from '@tanstack/react-query'
import { createRouter } from '@tanstack/react-router'
import { setupRouterSsrQueryIntegration } from '@tanstack/react-router-ssr-query'
import { routeTree } from './routeTree.gen'
import { initSentryTanStackClient } from './sentry.tanstack'
import { getQueryClient } from '@/data/query-client'
import { parseSearch, stringifySearch } from '@/lib/router-search-params'
import { installTranslationDomGuard } from '@/lib/translation-dom-guard'
export interface RouterContext {
queryClient: QueryClient
}
function getContext(): RouterContext {
return {
queryClient: getQueryClient(),
}
}
export function getRouter() {
const context = getContext()
const router = createRouter({
routeTree,
context,
scrollRestoration: true,
// `HashScrollController` owns hash scrolling (it also handles late-mounting targets)
defaultHashScrollIntoView: false,
defaultPreload: 'intent',
// Next-style search params (plain strings, repeated keys → arrays)
// instead of TanStack's JSON defaults, which coerce "2"→2/"true"→true
// and JSON-quote strings on write. The whole app — including nuqs and
// search-param readers — expects the Next semantics.
parseSearch,
stringifySearch,
// Inlined via Vite's `define` at build time; stays undefined (= app at `/`)
// unless NEXT_PUBLIC_BASE_PATH is set. Must agree with Vite `base`
basepath: process.env.NEXT_PUBLIC_BASE_PATH || undefined,
})
// Sentry init happens here — the earliest point with access to the router
// instance, which the tracing integration needs.
// No-op on the server and when no DSN is configured (see module).
initSentryTanStackClient(router)
// Keeps Google Translate's DOM rewrites from crashing the page (no-op on the server)
installTranslationDomGuard()
setupRouterSsrQueryIntegration({ router, queryClient: context.queryClient })
return router
}
declare module '@tanstack/react-router' {
interface Register {
router: ReturnType<typeof getRouter>
}
}