Skip to content

Commit

Permalink
fix: catch Google Translate DOM errors
Browse files Browse the repository at this point in the history
Monkey-patch `node.removeChild` and `node.insertBefore` to catch crashing errors generated by Google Translate. This should make projects usable in Chrome, in other languages, until the underlying bug is fixed.

See facebook/react#11538 (comment)
  • Loading branch information
eatyourgreens committed May 9, 2024
1 parent 9ca8f70 commit 71fb470
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/main.cjsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ ReactDOM = require 'react-dom'
apiClient = require 'panoptes-client/lib/api-client'
{ applyRouterMiddleware, Router, browserHistory } = require 'react-router'
useScroll = require 'react-router-scroll/lib/useScroll'
require('./monkeypatchNode')
{ routes } = require './router'
style = require '../css/main.styl'
{ Provider } = require('react-redux')
initStore = require('./redux/init-store').default
initSentry = require('./lib/init-sentry').default


# register locales
`import counterpart from 'counterpart';`
`import locales from './locales';`
Expand Down
28 changes: 28 additions & 0 deletions app/monkeypatchNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Monkey-patch node.removeChild and node.insertBefore,
to work around a crashing bug with Google Translate.
https://github.com/facebook/react/issues/11538#issuecomment-417504600
*/
if (typeof Node === 'function' && Node.prototype) {
const originalRemoveChild = Node.prototype.removeChild;
Node.prototype.removeChild = function(child) {
if (child.parentNode !== this) {
if (console) {
console.error('Cannot remove a child from a different parent', child, this);
}
return child;
}
return originalRemoveChild.apply(this, arguments);
}

const originalInsertBefore = Node.prototype.insertBefore;
Node.prototype.insertBefore = function(newNode, referenceNode) {
if (referenceNode && referenceNode.parentNode !== this) {
if (console) {
console.error('Cannot insert before a reference node from a different parent', referenceNode, this);
}
return newNode;
}
return originalInsertBefore.apply(this, arguments);
}
}

0 comments on commit 71fb470

Please sign in to comment.