patch DOM insertBefore and removeChild, workaround for DOM translators#2937
Merged
huumn merged 4 commits intostackernews:masterfrom Apr 27, 2026
Merged
patch DOM insertBefore and removeChild, workaround for DOM translators#2937huumn merged 4 commits intostackernews:masterfrom
huumn merged 4 commits intostackernews:masterfrom
Conversation
Soxasora
commented
Apr 26, 2026
Comment on lines
+9
to
+12
| // classes used by DOM translators | ||
| if (html.classList.contains('translated-ltr')) return true | ||
| if (html.classList.contains('translated-rtl')) return true | ||
| if (html.classList.contains('translated')) return true |
Member
Author
There was a problem hiding this comment.
*only Chromium-based DOM translations, needs to be tested on Safari and Firefox.
Soxasora
commented
Apr 26, 2026
Comment on lines
+42
to
+49
| Node.prototype.insertBefore = function (newNode, referenceNode) { | ||
| if (referenceNode && referenceNode.parentNode !== this) { | ||
| if (translateActive) { | ||
| return newNode | ||
| } | ||
| } | ||
| return originalInsertBefore.call(this, newNode, referenceNode) | ||
| } |
Member
Author
There was a problem hiding this comment.
We could technically fallback to appendChild here instead of creating phantom DOM nodes.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit eebae6b. Configure here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
Fixes #2936 with a workaround.
This PR patches
Node.removeChildandNode.insertBeforewith a widely-used community patch that silences theDOMExceptionthrown by them.A
MutationObserveris used to track whether the user is using a DOM Translator, this way the patch is applied only to DOM Translation users.Screenshots
patched-dom-translation.mov
Additional Context
Of course it's not a good idea to silence
DOMExceptions thrown by React, this bug appeared in 2017 for the first time and has never been fixed since: facebook/react#11538The original community patch involved silencing these exception globally, but I personally think it's better to be conservative and silence these exceptions only for DOM Translation users, as the tools they use already mess up the DOM, a DOM drift won't be the end of the world. Even if there's a DOM drift, React will fix it on the next render.
Other methods
translate=noon every impacted node, making sure that translation will still workChecklist
Are your changes backward compatible? Please answer below:
Yes
On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
6, tested only with Google Chrome's Translate feature.
For frontend changes: Tested on mobile, light and dark mode? Please answer below:
n/a
Did you introduce any new environment variables? If so, call them out explicitly here:
n/a
Did you use AI for this? If so, how much did it assist you?
Observer suggestion
Note
Medium Risk
Patches global DOM
Nodeprototype methods, which can have wide-reaching side effects if the translation detection is wrong or interacts unexpectedly with other code, though it is gated to only activate while translation appears enabled.Overview
Adds
patchDOMTranslations()(newlib/patch-dom-translate.js) that detects DOM translation via<html>classes and uses aMutationObserverto track when translation is active.When active, it monkey-patches
Node.removeChildandNode.insertBeforeto suppress/adjust operations that would otherwise throwNotFoundErrordue to translator-modified DOM, and wires this patch to run globally frompages/_app.js.Reviewed by Cursor Bugbot for commit 502e817. Bugbot is set up for automated code reviews on this repo. Configure here.