BoxcomputeboxyProblem & solution
Fixing the Google Translate crash in React without turning translation off
Summary
React applications crash with NotFoundError when Google Translate replaces text nodes with elements, because React still holds references to the detached nodes. The standard workaround of patching removeChild and insertBefore to skip detached nodes stops the crash but leaves stale translated text visible and new elements invisible. A mutation observer detects translation by monitoring for class=“translated-ltr” on , which appears about 190 ms before text swapping begins. The fix uses a WeakMap to pair each detached text node with its replacement , so removing the text removes its font and inserting before the text inserts before its font. The patches are only installed after translation is detected, leaving native DOM methods on untranslated pages so real bugs still reach error tracking.
Context
React components that update the DOM after Google Translate has replaced text nodes with elements crash with NotFoundError because React holds references to detached nodes. The standard workaround of patching removeChild and insertBefore to skip detached nodes stops the crash but leaves stale translated text visible and new elements invisible.
Approach / What changed
A mutation observer detects when Google Translate sets class=“translated-ltr” on . A WeakMap records each text node’s replacement from the mutation record. The patched removeChild and insertBefore use the font as a stand-in: removing a text node removes its font, and inserting before a text node inserts before its font. Text updates restore the original node with the new value. The patches are only installed after translation is detected, leaving native DOM methods on untranslated pages.
Takeaways
Google Translate inserts a before each text node and removes the original, causing React to hold references to detached nodes that throw NotFoundError on update.
The standard skip patch for removeChild and insertBefore stops the crash but leaves stale translated text visible and new elements invisible.
A mutation observer that detects class=“translated-ltr” on can install DOM patches only when translation is active, preserving native behavior on untranslated pages.
FAQ
Why does Google Translate cause React crashes?
Google Translate inserts a <font> holding the translation immediately before each text node, then removes the original text node. React still holds a reference to the removed node, so subsequent DOM operations on it throw NotFoundError.
How does the font stand-in approach work?
The fix patches removeChild and insertBefore to use the replacement <font> as a stand-in for the detached text node. Removing the text removes its font, and inserting before the text inserts before its font. Text updates restore the original node with the new value.
When are the DOM patches applied?
The patches are only installed after a mutation observer detects that Google Translate has set class="translated-ltr" on <html>, which occurs about 190 ms before text swapping begins. Untranslated pages keep native DOM methods, so real bugs still crash and reach error tracking.
Source and freshness
- Source
- boxcompute.ai
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.