Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(frontend): prevent React crashes from browser translation DOM mutation (#6613)
Celal Zamanoğlu3b57f2671305
diff --git a/skyvern-frontend/src/main.tsx b/skyvern-frontend/src/main.tsxindex 1a0ad689a..2fb69d63a 100644--- a/skyvern-frontend/src/main.tsx+++ b/skyvern-frontend/src/main.tsx@@ -3,7 +3,9 @@ import ReactDOM from "react-dom/client";import App from "./App.tsx";import "./index.css";import { installChunkLoadErrorHandler } from "./util/lazyWithReload";+import { installTranslationCrashGuard } from "./util/translationCrashGuard";+installTranslationCrashGuard();installChunkLoadErrorHandler();ReactDOM.createRoot(document.getElementById("root")!).render(diff --git a/skyvern-frontend/src/util/translationCrashGuard.test.ts b/skyvern-frontend/src/util/translationCrashGuard.test.tsnew file mode 100644index 000000000..118701847--- /dev/null+++ b/skyvern-frontend/src/util/translationCrashGuard.test.ts@@ -0,0 +1,74 @@+import { afterEach, beforeEach, describe, expect, it } from "vitest";++import { installTranslationCrashGuard } from "./translationCrashGuard";++describe("installTranslationCrashGuard", () => {+ beforeEach(() => {+ installTranslationCrashGuard();+ });++ afterEach(() => {+ document.body.innerHTML = "";+ });++ it("removes a genuine child normally", () => {+ const parent = document.createElement("div");+ const child = document.createElement("span");+ parent.appendChild(child);++ expect(parent.removeChild(child)).toBe(child);+ expect(parent.childNodes.length).toBe(0);+ });++ it("no-ops removeChild when the node belongs to another parent", () => {+ const parent = document.createElement("div");+ const otherParent = document.createElement("div");+ const displaced = document.createElement("span");+ otherParent.appendChild(displaced);++ expect(() => parent.removeChild(displaced)).not.toThrow();+ expect(parent.removeChild(displaced)).toBe(displaced);+ expect(displaced.parentNode).toBe(otherParent);+ });++ it("inserts before a genuine reference node normally", () => {+ const parent = document.createElement("div");+ const ref = document.createElement("span");+ const node = document.createElement("b");+ parent.appendChild(ref);++ parent.insertBefore(node, ref);+ expect(parent.firstChild).toBe(node);+ expect(parent.childNodes[1]).toBe(ref);+ });++ it("appends instead of throwing when the reference node has another parent", () => {+ const parent = document.createElement("div");+ parent.appendChild(document.createElement("i"));+ const displacedRef = document.createElement("span");+ document.createElement("div").appendChild(displacedRef);+ const node = document.createElement("b");++ expect(() => parent.insertBefore(node, displacedRef)).not.toThrow();+ expect(node.parentNode).toBe(parent);+ expect(parent.lastChild).toBe(node);+ });++ it("treats a null reference node as an append", () => {+ const parent = document.createElement("div");+ const node = document.createElement("b");++ expect(parent.insertBefore(node, null)).toBe(node);+ expect(parent.firstChild).toBe(node);+ });++ it("is idempotent and does not re-wrap the patched methods", () => {+ const patchedRemove = Node.prototype.removeChild;+ const patchedInsert = Node.prototype.insertBefore;++ installTranslationCrashGuard();++ expect(Node.prototype.removeChild).toBe(patchedRemove);+ expect(Node.prototype.insertBefore).toBe(patchedInsert);+ });+});diff --git a/skyvern-frontend/src/util/translationCrashGuard.ts b/skyvern-frontend/src/util/translationCrashGuard.tsnew file mode 100644index 000000000..df24a45b9--- /dev/null+++ b/skyvern-frontend/src/util/translationCrashGuard.ts@@ -0,0 +1,34 @@+let installed = false;++export function installTranslationCrashGuard(): void {+ if (installed) return;+ if (typeof Node === "undefined" || !Node.prototype) return;+ installed = true;++ const originalRemoveChild = Node.prototype.removeChild;+ Node.prototype.removeChild = function <T extends Node>(+ this: Node,+ child: T,+ ): T {+ // Translation extensions reparent text nodes, so React's stale reference+ // would throw NotFoundError here; no-op when the node is not our child.+ if (child.parentNode !== this) {+ return child;+ }+ return originalRemoveChild.call(this, child) as T;+ };++ const originalInsertBefore = Node.prototype.insertBefore;+ Node.prototype.insertBefore = function <T extends Node>(+ this: Node,+ node: T,+ referenceNode: Node | null,+ ): T {+ // The reference node was moved out by a translation extension; append+ // instead of throwing NotFoundError so the node still mounts.+ if (referenceNode && referenceNode.parentNode !== this) {+ return originalInsertBefore.call(this, node, null) as T;+ }+ return originalInsertBefore.call(this, node, referenceNode) as T;+ };+}