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
81 lines
3.1 KiB
TypeScript
81 lines
3.1 KiB
TypeScript
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
|
|
import {
|
|
installTranslationDomGuard,
|
|
resetTranslationDomGuardForTests,
|
|
type NodeLike,
|
|
} from './translation-dom-guard'
|
|
|
|
type FakeNode = { parentNode: FakeNode | null }
|
|
|
|
type FakeProto = {
|
|
removeChild: { call: (self: FakeNode, child: FakeNode) => unknown }
|
|
insertBefore: { call: (self: FakeNode, node: FakeNode, ref: FakeNode | null) => unknown }
|
|
}
|
|
|
|
function setup({ isTranslated }: { isTranslated: boolean }) {
|
|
const nativeRemoveChild = vi.fn((child: FakeNode) => child)
|
|
const nativeInsertBefore = vi.fn((node: FakeNode) => node)
|
|
const nodeConstructor = {
|
|
prototype: { removeChild: nativeRemoveChild, insertBefore: nativeInsertBefore },
|
|
}
|
|
installTranslationDomGuard({
|
|
nodeConstructor: nodeConstructor as unknown as NodeLike,
|
|
isTranslated: () => isTranslated,
|
|
})
|
|
const proto = nodeConstructor.prototype as unknown as FakeProto
|
|
return { proto, nativeRemoveChild, nativeInsertBefore }
|
|
}
|
|
|
|
describe('installTranslationDomGuard', () => {
|
|
beforeEach(() => {
|
|
resetTranslationDomGuardForTests()
|
|
})
|
|
|
|
test('skips removeChild for a detached node on a translated page', () => {
|
|
const { proto, nativeRemoveChild } = setup({ isTranslated: true })
|
|
const parent: FakeNode = { parentNode: null }
|
|
const child: FakeNode = { parentNode: { parentNode: null } }
|
|
|
|
expect(proto.removeChild.call(parent, child)).toBe(child)
|
|
expect(nativeRemoveChild).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('appends instead of dropping the node when the reference node is detached', () => {
|
|
const { proto, nativeInsertBefore } = setup({ isTranslated: true })
|
|
const parent: FakeNode = { parentNode: null }
|
|
const newNode: FakeNode = { parentNode: null }
|
|
const reference: FakeNode = { parentNode: { parentNode: null } }
|
|
|
|
proto.insertBefore.call(parent, newNode, reference)
|
|
expect(nativeInsertBefore).toHaveBeenCalledWith(newNode, null)
|
|
})
|
|
|
|
test('leaves the native error in place on an untranslated page', () => {
|
|
const { proto, nativeRemoveChild, nativeInsertBefore } = setup({ isTranslated: false })
|
|
const parent: FakeNode = { parentNode: null }
|
|
const orphan: FakeNode = { parentNode: { parentNode: null } }
|
|
|
|
proto.removeChild.call(parent, orphan)
|
|
proto.insertBefore.call(parent, { parentNode: null }, orphan)
|
|
|
|
expect(nativeRemoveChild).toHaveBeenCalledWith(orphan)
|
|
expect(nativeInsertBefore).toHaveBeenCalledWith(expect.anything(), orphan)
|
|
})
|
|
|
|
test('passes valid calls through untouched, including a null reference node', () => {
|
|
const { proto, nativeRemoveChild, nativeInsertBefore } = setup({ isTranslated: true })
|
|
const parent: FakeNode = { parentNode: null }
|
|
const child: FakeNode = { parentNode: parent }
|
|
const newNode: FakeNode = { parentNode: null }
|
|
|
|
proto.removeChild.call(parent, child)
|
|
proto.insertBefore.call(parent, newNode, child)
|
|
proto.insertBefore.call(parent, newNode, null)
|
|
|
|
expect(nativeRemoveChild).toHaveBeenCalledWith(child)
|
|
expect(nativeInsertBefore).toHaveBeenNthCalledWith(1, newNode, child)
|
|
expect(nativeInsertBefore).toHaveBeenNthCalledWith(2, newNode, null)
|
|
})
|
|
})
|