Skip to content

[FSSDK-13343] Skip the default browser event store when localStorage is unavailable - #1184

Open
Raju Ahmed (raju-opti) wants to merge 1 commit into
masterfrom
raju/local-storage-availability
Open

Raju Ahmed (raju-opti) wants to merge 1 commit into
masterfrom
raju/local-storage-availability

Conversation

@raju-opti

@raju-opti Raju Ahmed (raju-opti) commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • localStorage check: new isLocalStorageAvailable() in lib/utils/env/browser.ts. It returns false when reading the localStorage global throws (a ReferenceError where it isn't defined, such as SSR, or a SecurityError where storage is blocked, such as disabled cookies or sandboxed iframes) or when the global is null.
  • Browser batch event processor: createBatchEventProcessor only builds its default LocalStorageCache-backed EventStore when localStorage is available. Otherwise it runs without an event store and adds a Warn startup log: localStorage is not available, events will not be persisted. A user-provided eventStore is used exactly as before, whether or not localStorage is available.
  • Startup logs from platform factories: getBatchEventProcessor accepts a startupLogs option and puts those logs before its own, without changing the caller's array. The option is on the internal BatchEventProcessorFactoryOptions only; the public BatchEventProcessorOptions is unchanged.

Before this change, the browser factory always created a LocalStorageCache event store. Where localStorage can't be accessed, every store operation threw. With no event store, events are still batched and dispatched; failed events just aren't persisted for retry across page loads.

Test plan

  • New lib/utils/env/browser.spec.ts: localStorage that is accessible, null, not defined (ReferenceError), and blocked (SecurityError).
  • event_processor_factory.browser.spec.ts: no event store and a startup warning when localStorage is unavailable; no warning when it is available; a provided eventStore is wrapped with getPrefixEventStore whether or not localStorage is available, with no warning.
  • event_processor_factory.spec.ts: provided startupLogs are included with the processor's own, and the input array isn't changed.
  • Ran the three spec files locally (41 tests pass), plus tsc --noEmit, eslint on the changed files and validate-platform-isolation.

Issues

🤖 Generated with Claude Code

…is unavailable

The browser batch event processor now checks whether localStorage can be
accessed before creating its default LocalStorageCache-backed EventStore.
If it can't (SSR, storage blocked by browser settings or a sandboxed
iframe), the processor runs without an event store and logs a startup
warning. A user-provided eventStore is used as before.

getBatchEventProcessor accepts startupLogs from platform factories and
prepends them to its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 77.554% (-0.07%) from 77.624% — raju/local-storage-availability into master

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused fallback preserves existing optional-store behavior, regression tests cover the changes, and no blocking issues were identified.

No open issues 🎉

What changed in this PR

This PR addresses #1182 by skipping the browser’s default persistent event store when localStorage is inaccessible, while preserving event batching, dispatch, and custom event stores.

Changes:

  • Adds a guarded localStorage availability check and a startup warning when persistence is disabled.
  • Allows platform factories to supply startup logs without mutating the caller’s array.
  • Adds regression tests for storage availability, custom stores, and startup-log propagation.
File Description
lib/​utils/​env/​browser.ts Adds the guarded storage availability check.
lib/​utils/​env/​browser.spec.ts Tests accessible, null, missing, and blocked storage.
lib/​message/​error_message.ts Defines the persistence-disabled warning.
lib/​event_processor/​event_processor_factory.ts Copies supplied startup logs before adding processor logs.
lib/​event_processor/​event_processor_factory.spec.ts Tests log propagation and input-array preservation.
lib/​event_processor/​event_processor_factory.browser.ts Conditionally creates the default store and supplies the warning.
lib/​event_processor/​event_processor_factory.browser.spec.ts Tests fallback behavior and custom-store preservation.

Review effort: Balanced

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good overall. Should we add a catch in lib/event_processor/batch_event_processor.ts:275 ? The localStorage check covers the default store, but a user-provided eventStore that rejects would still cause the unhandled rejection!

@raju-opti

Copy link
Copy Markdown
Contributor Author

Looks good overall. Should we add a catch in lib/event_processor/batch_event_processor.ts:275 ? The localStorage check covers the default store, but a user-provided eventStore that rejects would still cause the unhandled rejection!

In that case, I would say the responsibility to ensure that the store that is passed in works is on the user.

Md Junaed Hossain (@junaed-optimizely)

This branch has not been deployed

No deployments
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.

4 participants