Skip to content

patch DOM insertBefore and removeChild, workaround for DOM translators#2937

Merged
huumn merged 4 commits intostackernews:masterfrom
Soxasora:fix/react-dom-translations
Apr 27, 2026
Merged

patch DOM insertBefore and removeChild, workaround for DOM translators#2937
huumn merged 4 commits intostackernews:masterfrom
Soxasora:fix/react-dom-translations

Conversation

@Soxasora
Copy link
Copy Markdown
Member

@Soxasora Soxasora commented Apr 26, 2026

Description

Fixes #2936 with a workaround.
This PR patches Node.removeChild and Node.insertBefore with a widely-used community patch that silences the DOMException thrown by them.
A MutationObserver is 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#11538

The 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

  • Disabling translation site-wide
  • Carefully disseminate translate=no on every impacted node, making sure that translation will still work

Checklist

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 Node prototype 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() (new lib/patch-dom-translate.js) that detects DOM translation via <html> classes and uses a MutationObserver to track when translation is active.

When active, it monkey-patches Node.removeChild and Node.insertBefore to suppress/adjust operations that would otherwise throw NotFoundError due to translator-modified DOM, and wires this patch to run globally from pages/_app.js.

Reviewed by Cursor Bugbot for commit 502e817. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread lib/patch-dom-translate.js Outdated
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
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*only Chromium-based DOM translations, needs to be tested on Safari and Firefox.

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)
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could technically fallback to appendChild here instead of creating phantom DOM nodes.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread lib/patch-dom-translate.js
@huumn huumn merged commit f366774 into stackernews:master Apr 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOM Translators desync React's reconciler

2 participants