Skip to content

Commit 531d2f3

Browse files
authored
fix: add patch to removeChild to address hydration errors (#207)
1 parent cd2190a commit 531d2f3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

packages/experiment-tag/src/experiment.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
import { setMarketingCookie } from './util/cookie';
3434
import { getInjectUtils } from './util/inject-utils';
3535
import { VISUAL_EDITOR_SESSION_KEY, WindowMessenger } from './util/messenger';
36+
import { patchRemoveChild } from './util/patch';
3637
import {
3738
getStorageItem,
3839
setStorageItem,
@@ -190,6 +191,7 @@ export class DefaultWebExperimentClient implements WebExperimentClient {
190191
if (this.isRunning) {
191192
return;
192193
}
194+
patchRemoveChild();
193195
const urlParams = getUrlParams();
194196
this.isVisualEditorMode =
195197
urlParams['VISUAL_EDITOR'] === 'true' ||
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Patch removeChild to avoid errors when removing nodes that are added
3+
* mutate/inject actions.
4+
*/
5+
export const patchRemoveChild = () => {
6+
HTMLElement.prototype.removeChild = function <T extends Node>(n: T): T {
7+
if (!n || n.parentNode === this) {
8+
return Node.prototype.removeChild.call(this, n) as T;
9+
}
10+
return n;
11+
};
12+
};

0 commit comments

Comments
 (0)