-
Notifications
You must be signed in to change notification settings - Fork 7
feat: avoid Google Translate plugin crashes #4203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e089c9c
7baccba
0cf1902
5509d5d
9e23b4a
4f11f68
a628164
ffa7631
446f3e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { startTransition, StrictMode } from "react"; | ||
| import { hydrateRoot } from "react-dom/client"; | ||
| import { HydratedRouter } from "react-router/dom"; | ||
|
|
||
| import { fixExternalDOMMutationsCrashes } from "./utils/helpers/domMutation.utils"; | ||
|
|
||
| // Prevent crashes from browser extensions like Google Translate | ||
| fixExternalDOMMutationsCrashes(); | ||
|
|
||
| startTransition(() => { | ||
| hydrateRoot( | ||
| document, | ||
| <StrictMode> | ||
| <HydratedRouter /> | ||
| </StrictMode>, | ||
| ); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Guards against React crashes when the DOM is manipulated by extensions like Google Translate | ||
| // ? Reference: https://github.com/facebook/react/issues/11538#issuecomment-417504600 | ||
| declare global { | ||
| interface Window { | ||
| __renku_appliedDOMFix?: boolean; | ||
| } | ||
| } | ||
|
|
||
| export function fixExternalDOMMutationsCrashes() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I have seen implementations use a variable on if (typeof window === 'object' && window.__renku_appliedDOMFix == null) {
// ...body...
window.__renku_appliedDOMFix = true;
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good suggestion to prevent accidental double run. Worst case, it's an unnecessary safeguard |
||
| // Prevent invoking the code twice | ||
| if (typeof window !== "object" || window.__renku_appliedDOMFix) return; | ||
|
|
||
| if (typeof Node === "function" && Node.prototype) { | ||
| const originalRemoveChild = Node.prototype.removeChild; | ||
| Node.prototype.removeChild = function <T extends Node>(child: T): T { | ||
| if (child.parentNode !== this) { | ||
| if (console) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| "Cannot remove a child from a different parent", | ||
| child, | ||
| this, | ||
| ); | ||
| } | ||
| return child; | ||
| } | ||
| return originalRemoveChild.call(this, child) as T; | ||
| }; | ||
|
leafty marked this conversation as resolved.
|
||
|
|
||
| const originalInsertBefore = Node.prototype.insertBefore; | ||
| Node.prototype.insertBefore = function <T extends Node>( | ||
| newNode: T, | ||
| referenceNode: Node | null, | ||
| ): T { | ||
| if (referenceNode && referenceNode.parentNode !== this) { | ||
| if (console) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| "Cannot insert before a reference node from a different parent", | ||
| referenceNode, | ||
| this, | ||
| ); | ||
| } | ||
| return newNode; | ||
| } | ||
| return originalInsertBefore.call(this, newNode, referenceNode) as T; | ||
| }; | ||
| } | ||
|
leafty marked this conversation as resolved.
|
||
|
|
||
| window.__renku_appliedDOMFix = true; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep the original implementation from React.
fixExternalDOMMutationsCrashes()consolewe can also consider sendingwarningorinfoto Sentry.