Skip to content

[FSSDK-12471] provider fix#332

Open
junaed-optimizely wants to merge 1 commit intomasterfrom
junaed/fssdk-12471-provider-fixes
Open

[FSSDK-12471] provider fix#332
junaed-optimizely wants to merge 1 commit intomasterfrom
junaed/fssdk-12471-provider-fixes

Conversation

@junaed-optimizely
Copy link
Copy Markdown
Collaborator

Summary

  • Remove the cleanup effect that called store.reset() and UCM.dispose() on unmount
  • Merge the onReady and CONFIG_UPDATE effects into a single effect with a configReceived flag

StrictMode compatibility (React 18+):

In dev mode, StrictMode runs effect cleanup→remount without re-running the render body. The cleanup effect called store.reset() (clearing userContext to null) and UCM.dispose() (aborting in-flight async operations like ODP segment fetches). Since the Provider doesn't re-render after effect remount, nobody restores the state — hooks stay stuck in isLoading: true forever. On real unmount, the store is garbage collected with the component tree, so eager reset is unnecessary.

Cached datafile race condition:

When the datafile response is served from the browser HTTP cache, the polling config manager loads it before the effect subscribes its CONFIG_UPDATE listener. Since the config doesn't change after subscription, the notification is never re-emitted, and hooks never transition from hasConfig: false to true. onReady().then() acts as a fallback for this case; the configReceived flag prevents a double-refresh when both fire on a cold cache.

Test plan

  • Updated existing Provider specs to reflect new unmount behavior (store state preserved, not eagerly reset)
  • Manually verified in a Next.js 15 + React 19 and React 17 app

Issues

FSSDK-12471

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates OptimizelyProvider lifecycle behavior to be compatible with React 18+ StrictMode and to prevent a race where cached datafiles can cause hooks to remain stuck in a “loading / no-config” state.

Changes:

  • Remove the unmount cleanup behavior that eagerly reset the provider store / disposed the user-context manager.
  • Merge onReady() handling and OPTIMIZELY_CONFIG_UPDATE subscription into a single effect, with a configReceived flag to avoid double refreshes.
  • Update provider unit tests to reflect the new unmount semantics.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/provider/OptimizelyProvider.tsx Consolidates readiness + config-update handling and removes the unmount reset/dispose cleanup that breaks StrictMode remount behavior.
src/provider/OptimizelyProvider.spec.tsx Updates cleanup-related expectations/descriptions to align with the new unmount behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +249 to 253
// Store state is preserved — on real unmount, the store is
// garbage collected with the component tree. Eagerly resetting
// breaks React 18+ StrictMode (effect cleanup destroys state
// that the render body set, and no re-render restores it).
expect(store.getState().error).toBeNull();
Comment on lines +249 to +252
// Store state is preserved — on real unmount, the store is
// garbage collected with the component tree. Eagerly resetting
// breaks React 18+ StrictMode (effect cleanup destroys state
// that the render body set, and no re-render restores it).
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.

2 participants