Skip to content

Fix LocalStorageAssignmentShim reading localStorage["undefined"] during construction#278

Merged
bertilhatt merged 3 commits intomainfrom
fix/local-storage-shim-undefined-key
Mar 27, 2026
Merged

Fix LocalStorageAssignmentShim reading localStorage["undefined"] during construction#278
bertilhatt merged 3 commits intomainfrom
fix/local-storage-shim-undefined-key

Conversation

@bertilhatt
Copy link
Copy Markdown
Contributor

Summary

  • Bug: LocalStorageAssignmentShim's class field initializers for size and [Symbol.toStringTag] called this.getCache() before the constructor body assigned this.localStorageKey. Per JS class semantics, field initializers run before the constructor body, so this.localStorageKey was undefined and getCache() read localStorage.getItem("undefined"). If that key held non-JSON data (e.g. from third-party scripts), JSON.parse threw and the SDK failed to initialize.
  • Fix: Convert both field initializers to getters. This avoids the eager getCache() call during construction entirely, and also fixes a secondary bug where size was a stale one-time snapshot that never reflected mutations.
  • Test: Added a regression test that sets localStorage["undefined"] to a non-JSON value and verifies LocalStorageAssignmentCache construction does not throw.

Jira: FFESUPPORT-610

Test plan

  • New regression test: constructs cache when localStorage["undefined"] contains non-JSON — must not throw
  • All existing tests pass (153/153)

Made with Cursor

…ng construction

Class field initializers for `size` and `[Symbol.toStringTag]` called
`this.getCache()` before the constructor body assigned `this.localStorageKey`,
causing `localStorage.getItem("undefined")`. If that key held non-JSON data,
`JSON.parse` threw and the SDK failed to initialize.

Convert both to getters so they no longer eagerly read localStorage during
construction. This also fixes a secondary bug where `size` was a stale
snapshot that never updated after mutations.

FFESUPPORT-610

Made-with: Cursor
Comment thread src/cache/local-storage-assignment-shim.ts Outdated
Comment thread src/cache/local-storage-assignment-shim.ts
bertilhatt and others added 2 commits March 27, 2026 10:24
Co-authored-by: Oleksii Shmalko <oleksii.shmalko@datadoghq.com>
Remove the explicit `string` type annotation from `[Symbol.toStringTag]`
to satisfy `@typescript-eslint/no-inferrable-types` while keeping the
reviewed field-style implementation.

Made-with: Cursor
@bertilhatt bertilhatt merged commit 19405ce into main Mar 27, 2026
8 checks passed
@bertilhatt bertilhatt deleted the fix/local-storage-shim-undefined-key branch March 27, 2026 09:45
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