diff --git a/ui/src/app/flags/streamingUtils.ts b/ui/src/app/flags/streamingUtils.ts new file mode 100644 index 0000000000..dd0246d781 --- /dev/null +++ b/ui/src/app/flags/streamingUtils.ts @@ -0,0 +1,7 @@ +/** + * Stream payloads without an explicit `type` can still represent data updates. + * We only skip invalidation for explicit error events. + */ +export const shouldInvalidateFromStreamEvent = ( + eventData: { type?: string; etag?: string } | null +) => eventData !== null && eventData.type !== 'error'; diff --git a/ui/src/store.test.ts b/ui/src/store.test.ts new file mode 100644 index 0000000000..2d0d142c21 --- /dev/null +++ b/ui/src/store.test.ts @@ -0,0 +1,21 @@ +import { shouldInvalidateFromStreamEvent } from '~/app/flags/streamingUtils'; + +describe('shouldInvalidateFromStreamEvent', () => { + it('invalidates for refetchEvaluation events', () => { + expect(shouldInvalidateFromStreamEvent({ type: 'refetchEvaluation' })).toBe( + true + ); + }); + + it('invalidates for stream payloads without a type', () => { + expect(shouldInvalidateFromStreamEvent({ etag: 'digest' })).toBe(true); + }); + + it('does not invalidate for error events', () => { + expect(shouldInvalidateFromStreamEvent({ type: 'error' })).toBe(false); + }); + + it('does not invalidate for null payloads', () => { + expect(shouldInvalidateFromStreamEvent(null)).toBe(false); + }); +}); diff --git a/ui/src/store.ts b/ui/src/store.ts index aeb56bfd0d..ac4b164b92 100644 --- a/ui/src/store.ts +++ b/ui/src/store.ts @@ -14,6 +14,7 @@ import { eventKey, eventSlice } from '~/app/events/eventSlice'; import { analyticsApi } from '~/app/flags/analyticsApi'; import { flagsApi, flagsTableSlice } from '~/app/flags/flagsApi'; import { eventReceived, streamingReducer } from '~/app/flags/streamingApi'; +import { shouldInvalidateFromStreamEvent } from '~/app/flags/streamingUtils'; import { metaSlice } from '~/app/meta/metaSlice'; import { namespaceApi, @@ -114,8 +115,8 @@ listenerMiddleware.startListening({ etag?: string; } | null; - if (eventData?.type !== 'refetchEvaluation') { - console.warn('unexpected sse event', eventData); + if (!shouldInvalidateFromStreamEvent(eventData)) { + console.warn('skipping cache invalidation for sse event', eventData); return; }