mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## 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
59 lines
2.0 KiB
TypeScript
59 lines
2.0 KiB
TypeScript
import { isPageTranslated } from 'common/sentry'
|
|
|
|
export type NodeLike = {
|
|
prototype: {
|
|
removeChild(child: Node): unknown
|
|
insertBefore(newNode: Node, referenceNode: Node | null): unknown
|
|
}
|
|
}
|
|
|
|
interface InstallOptions {
|
|
nodeConstructor?: NodeLike
|
|
isTranslated?: () => boolean
|
|
}
|
|
|
|
let isInstalled = false
|
|
|
|
/**
|
|
* Google Translate replaces text nodes with `<font>` wrappers. React still holds the original
|
|
* text node, so a later `removeChild` / `insertBefore` against it throws a `NotFoundError` and
|
|
* takes down the whole page (facebook/react#11538).
|
|
*
|
|
* Narrow on purpose: it only steps in when the page is translated AND the node is not where the
|
|
* caller expects it. Every other call, including the same failure on an untranslated page,
|
|
* goes to the native method untouched so real bugs still surface.
|
|
*
|
|
* - `removeChild`: the node is already gone from `this`, so skip.
|
|
* - `insertBefore`: the reference node is gone, so append instead of dropping the new node.
|
|
*/
|
|
export function installTranslationDomGuard({
|
|
nodeConstructor = typeof Node === 'undefined' ? undefined : Node,
|
|
isTranslated = isPageTranslated,
|
|
}: InstallOptions = {}) {
|
|
if (!nodeConstructor || isInstalled) return
|
|
isInstalled = true
|
|
|
|
const nativeRemoveChild = nodeConstructor.prototype.removeChild
|
|
const nativeInsertBefore = nodeConstructor.prototype.insertBefore
|
|
|
|
nodeConstructor.prototype.removeChild = function removeChild(this: Node, child: Node) {
|
|
if (child && child.parentNode !== this && isTranslated()) return child
|
|
return nativeRemoveChild.call(this, child)
|
|
}
|
|
|
|
nodeConstructor.prototype.insertBefore = function insertBefore(
|
|
this: Node,
|
|
newNode: Node,
|
|
referenceNode: Node | null
|
|
) {
|
|
if (referenceNode && referenceNode.parentNode !== this && isTranslated()) {
|
|
return nativeInsertBefore.call(this, newNode, null)
|
|
}
|
|
return nativeInsertBefore.call(this, newNode, referenceNode)
|
|
}
|
|
}
|
|
|
|
export function resetTranslationDomGuardForTests() {
|
|
isInstalled = false
|
|
}
|