Skip to content

Commit 175eff3

Browse files
Copilotfrostaura
andauthored
Guard service worker cache refresh listener wiring
Agent-Logs-Url: https://github.com/frostaura/fa.lifeos/sessions/8570f3e6-1dfd-4519-8939-c252040f6a12 Co-authored-by: frostaura <8956241+frostaura@users.noreply.github.com>
1 parent a318fd2 commit 175eff3

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/frontend/src/hooks/__tests__/useServiceWorkerCacheRefresh.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,22 @@ describe('useServiceWorkerCacheRefresh', () => {
120120

121121
expect(removeEventListenerMock).toHaveBeenCalledWith('message', expect.any(Function));
122122
});
123+
124+
it('stays idle when the service worker container does not expose event listeners', () => {
125+
Object.defineProperty(globalThis.navigator, 'serviceWorker', {
126+
configurable: true,
127+
value: {
128+
getRegistration: vi.fn(),
129+
},
130+
});
131+
132+
const { result, unmount } = renderHook(() => useServiceWorkerCacheRefresh());
133+
134+
expect(result.current.phase).toBe('idle');
135+
expect(addEventListenerMock).not.toHaveBeenCalled();
136+
137+
unmount();
138+
139+
expect(removeEventListenerMock).not.toHaveBeenCalled();
140+
});
123141
});

src/frontend/src/hooks/useServiceWorkerCacheRefresh.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export function useServiceWorkerCacheRefresh(enabled = true): ServiceWorkerCache
4242
return;
4343
}
4444

45+
const serviceWorkerContainer = navigator.serviceWorker;
46+
if (
47+
!serviceWorkerContainer
48+
|| typeof serviceWorkerContainer.addEventListener !== 'function'
49+
|| typeof serviceWorkerContainer.removeEventListener !== 'function'
50+
) {
51+
return;
52+
}
53+
4554
const invalidateSubscribedQueries = () => {
4655
dispatch(apiSlice.util.invalidateTags(apiSliceTagTypes.map((type) => ({ type }))));
4756
dispatch(identityApi.util.invalidateTags(identityApiTagTypes.map((type) => ({ type }))));
@@ -95,10 +104,10 @@ export function useServiceWorkerCacheRefresh(enabled = true): ServiceWorkerCache
95104
}, INVALIDATION_DEBOUNCE_MS);
96105
};
97106

98-
navigator.serviceWorker.addEventListener('message', handleMessage);
107+
serviceWorkerContainer.addEventListener('message', handleMessage);
99108

100109
return () => {
101-
navigator.serviceWorker.removeEventListener('message', handleMessage);
110+
serviceWorkerContainer.removeEventListener('message', handleMessage);
102111
activeRequestKeysRef.current.clear();
103112
if (invalidateTimerRef.current) {
104113
window.clearTimeout(invalidateTimerRef.current);

0 commit comments

Comments
 (0)