Skip to content

feat: avoid Google Translate plugin crashes#4203

Merged
lorenzo-cavazzi merged 9 commits into
mainfrom
lorenzo/support-translate
Jun 4, 2026
Merged

feat: avoid Google Translate plugin crashes#4203
lorenzo-cavazzi merged 9 commits into
mainfrom
lorenzo/support-translate

Conversation

@lorenzo-cavazzi
Copy link
Copy Markdown
Member

Avoid crashing the browser when Google Translate does its own thing with the DOM

/deploy renku=release-2.18.0
fix #4202

@RenkuBot
Copy link
Copy Markdown
Contributor

RenkuBot commented Jun 3, 2026

You can access the deployment of this PR at https://renku-ci-ui-4203.dev.renku.ch

@lorenzo-cavazzi lorenzo-cavazzi force-pushed the lorenzo/support-translate branch from bb8ae89 to 7baccba Compare June 4, 2026 09:06
Copy link
Copy Markdown
Member

@leafty leafty left a comment

Choose a reason for hiding this comment

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

The fix-method needs to run at the top of the client init script. At the moment the client entrypoint is hidden, so here is how to do it properly:

  1. Run $ npx react-router reveal in client/:
    Entry file entry.client created at src/entry.client.tsx.
    Entry file src/entry.server.tsx already exists.
    
  2. Open the new file src/entry.client.tsx and edit it:
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { HydratedRouter } from "react-router/dom";
import { fixExternalDOMMutationsCrashes } from "./utils/...";

// Prevent crashes from browser extensions, e.g. Google Translate
fixExternalDOMMutationsCrashes();

startTransition(() => {
  hydrateRoot(
    document,
    <StrictMode>
      <HydratedRouter />
    </StrictMode>,
  );
});

Copy link
Copy Markdown
Member

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.

  1. Add a comment like this:
    // Reference: https://github.com/facebook/react/issues/11538
    // This fixes React crashing when the DOM is manipulated by extensions, e.g. Google Translate
  2. Name the method something like fixExternalDOMMutationsCrashes()
  3. Use the implementation from Make React resilient to DOM mutations from Google Translate facebook/react#11538 (comment). As far as we know, it's the best implementation of the fix (from the React team).
  4. Instead of logging to console we can also consider sending warning or info to Sentry.

Comment thread client/src/root.tsx Outdated
import "./styles/renku_bootstrap.scss";
import "./utils/bootstrap/bootstrap.client";

removeChildGuard();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This runs in the server too, so this is not the right place for this method.

Comment thread client/src/entry.client.tsx Outdated
Comment on lines +7 to +17
startTransition(() => {
// Prevent crashes from browser extensions like Google Translate
fixExternalDOMMutationsCrashes();

hydrateRoot(
document,
<StrictMode>
<HydratedRouter />
</StrictMode>,
);
});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the method in startTransition() is executed more than once, then maybe this is better:

Suggested change
startTransition(() => {
// Prevent crashes from browser extensions like Google Translate
fixExternalDOMMutationsCrashes();
hydrateRoot(
document,
<StrictMode>
<HydratedRouter />
</StrictMode>,
);
});
// Prevent crashes from browser extensions like Google Translate
fixExternalDOMMutationsCrashes();
startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<HydratedRouter />
</StrictMode>,
);
});

We can check if it's better outside of the startTransition().

Copy link
Copy Markdown
Member

@leafty leafty Jun 4, 2026

Choose a reason for hiding this comment

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

Sorry, I am not sure if it's best to call before startTransition or inside it 😅

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.

It should run only once, let's try to leave it outside 5509d5d

Comment thread client/src/utils/helpers/domMutation.utils.ts
Comment thread client/src/utils/helpers/domMutation.utils.ts
@lorenzo-cavazzi lorenzo-cavazzi marked this pull request as ready for review June 4, 2026 11:37
leafty
leafty previously approved these changes Jun 4, 2026
Copy link
Copy Markdown
Member

@leafty leafty left a comment

Choose a reason for hiding this comment

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

LGTM, one non-blocking comment below.

@lorenzo-cavazzi lorenzo-cavazzi requested a review from a team as a code owner June 4, 2026 11:37
@lorenzo-cavazzi lorenzo-cavazzi enabled auto-merge (squash) June 4, 2026 11:37
@lorenzo-cavazzi lorenzo-cavazzi disabled auto-merge June 4, 2026 11:38
@@ -0,0 +1,40 @@
// Guards against React crashes when the DOM is manipulated by extensions like Google Translate
// ? Reference: https://github.com/facebook/react/issues/11538#issuecomment-417504600
export function fixExternalDOMMutationsCrashes() {
Copy link
Copy Markdown
Member

@leafty leafty Jun 4, 2026

Choose a reason for hiding this comment

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

Note: I have seen implementations use a variable on window to ensure this is executed only once, e.g.

if (typeof window === 'object' && window.__renku_appliedDOMFix == null) {
  // ...body...
  window.__renku_appliedDOMFix = 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.

That's a good suggestion to prevent accidental double run. Worst case, it's an unnecessary safeguard
ffa7631

Copy link
Copy Markdown
Member

@leafty leafty left a comment

Choose a reason for hiding this comment

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

LGTM, tested on chrome: I crash in dev with translate and with this PR I can use translate and it seems to not crash.

@lorenzo-cavazzi lorenzo-cavazzi merged commit 4efe7f5 into main Jun 4, 2026
22 of 23 checks passed
@lorenzo-cavazzi lorenzo-cavazzi deleted the lorenzo/support-translate branch June 4, 2026 14:58
@RenkuBot
Copy link
Copy Markdown
Contributor

RenkuBot commented Jun 4, 2026

Tearing down the temporary RenkuLab deplyoment for this PR.

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.

Cannot Create projects when translation to Chinese in turned on in the browser

3 participants