Commit fa1769c
authored
fix(frontend): prevent memory leak from uncleaned storage listener in useFeatureFlag (#1295)
## Root cause
`useFeatureFlag` called `window.addEventListener('storage', evaluate)` inside `useIsomorphicLayoutEffect` but **never returned a cleanup function**. Each time `flag` or `scrollTargetId` changed, React re-ran the effect — adding a new listener without removing the previous one. After `n` re-renders, `n` orphaned listener references accumulated in `window._eventListeners`, each retaining the `evaluate` closure (and with it `flag`, `scrollTargetId`, `setIsEnabled`, etc.) in memory.
## Fix (`src/hooks/useFeatureFlag.ts`)
1. Extract the body as a named `evaluate()` function so the **same reference** is passed to both `addEventListener` and `removeEventListener` (required for correct deregistration).
2. Return `() => window.removeEventListener('storage', evaluate)` from the effect so React cleans up on unmount **and** before every re-run triggered by a dep change.
```diff
- const newEnabled = getFeatureFlag(flag);
- setIsEnabled(newEnabled);
- …
+ function evaluate() {
+ const newEnabled = getFeatureFlag(flag);
+ setIsEnabled(newEnabled);
+ …
+ }
+
+ evaluate();
+
+ if (typeof window !== 'undefined') {
+ window.addEventListener('storage', evaluate);
+ return () => window.removeEventListener('storage', evaluate);
+ }
}, [flag, scrollTargetId]);
```
The `storage` listener also makes flag state reactive: toggling a flag via devtools or another tab now updates the component without a page reload.
## Regression tests (`src/hooks/__tests__/useFeatureFlag.test.ts`)
- **`removes storage listener on unmount so it does not leak`** — spies on `window.addEventListener`/`removeEventListener` and asserts the exact handler registered is also removed when the hook unmounts.
- **`re-evaluates the flag when a storage event fires`** — dispatches a `StorageEvent` after changing the mock return value and asserts the hook state updates.
Closes #12201 parent adaa4fd commit fa1769c
2 files changed
Lines changed: 64 additions & 9 deletions
Lines changed: 43 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
45 | 87 | | |
Lines changed: 21 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | | - | |
62 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
63 | 64 | | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
71 | 84 | | |
72 | 85 | | |
73 | 86 | | |
| |||
0 commit comments