Skip to content

fix: implement localStorage namespace isolation for multiple instances#8236

Open
vhenckel wants to merge 6 commits intonasa:masterfrom
vhenckel:fix/localstorage-namespace-isolation
Open

fix: implement localStorage namespace isolation for multiple instances#8236
vhenckel wants to merge 6 commits intonasa:masterfrom
vhenckel:fix/localstorage-namespace-isolation

Conversation

@vhenckel
Copy link
Copy Markdown
Contributor

Closes #8206

Describe your changes:

This PR implements automatic namespace isolation for localStorage operations in Open MCT to resolve LocalStorage contention issues when multiple instances run on the same host/port.

Summary of Changes:

  1. Created NamespacedLocalStorage utility (src/utils/NamespacedLocalStorage.js):

    • Wraps localStorage operations with namespace prefixing
    • Ensures all localStorage keys are prefixed with instance namespace
    • Prevents conflicts between multiple Open MCT instances
  2. Updated MCT class (src/MCT.js):

    • Added optional namespace parameter to constructor
    • Automatically generates unique namespace if not provided
    • Namespace persists across page reloads using sessionStorage
    • Added getNamespacedLocalStorage() method
  3. Updated StoragePersistence (src/api/user/StoragePersistence.js):

    • Converted from singleton to class
    • Accepts namespacedLocalStorage in constructor
    • All localStorage operations now use namespace isolation
  4. Updated UserAPI (src/api/user/UserAPI.js):

    • Initializes StoragePersistence with namespaced localStorage instance
    • Ensures user role storage is isolated per instance
  5. Updated LocalStorageObjectProvider (src/plugins/localStorage/LocalStorageObjectProvider.js):

    • Accepts optional namespacedLocalStorage parameter
    • Maintains backward compatibility (falls back to window.localStorage if not provided)
  6. Updated LocalStorage plugin (src/plugins/localStorage/plugin.js):

    • Automatically uses instance's namespaced localStorage
    • Ensures all persisted objects use namespace isolation

Key Features:

  • Automatic: Namespace is generated automatically for each instance
  • Persistent: Namespace persists across page reloads (same tab/window)
  • Isolated: Each browser tab/window gets its own namespace
  • Backward Compatible: Existing code continues to work without changes
  • Transparent: All localStorage operations are automatically namespaced

How It Works:

  • When an MCT instance is created, it automatically generates a unique namespace (e.g., mct-1768601842155-123456)
  • This namespace is persisted in sessionStorage for the same tab
  • All localStorage operations are prefixed with this namespace
  • Multiple tabs/windows each get their own namespace, preventing conflicts

Testing:

  • Created and tested multiple instances in separate browser tabs
  • Verified localStorage isolation (objects in one tab don't appear in another)
  • Verified persistence across page reloads (F5)
  • Verified backward compatibility with existing code

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Is this a notable change that will require a special callout in the release notes? For example, will this break compatibility with existing APIs or projects that consume these plugins?

Author Checklist

  • Changes address original issue?
  • Tests included and/or updated with changes?
  • Has this been smoke tested?
  • Have you associated this PR with a type: label? Note: this is not necessarily the same as the original issue.
  • Have you associated a milestone with this PR? Note: leave blank if unsure.
  • Testing instructions included in associated issue OR is this a dependency/testcase change?

- Add NamespacedLocalStorage utility to wrap localStorage with namespace prefixing
- Auto-generate unique namespace per MCT instance (persisted in sessionStorage)
- Update StoragePersistence to use namespaced localStorage
- Update UserAPI to initialize StoragePersistence with namespaced storage
- Update LocalStorageObjectProvider to accept optional namespaced storage
- Update LocalStorage plugin to use instance's namespaced storage
- Update pluginSpec tests to work with namespaced storage
- Maintain backward compatibility with existing code

Fixes nasa#8206

This ensures multiple Open MCT instances on the same host/port
don't conflict in localStorage while maintaining backward compatibility.
@vhenckel
Copy link
Copy Markdown
Contributor Author

@akhenry

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.

Multiple Open MCT instances on the same host causes LocalStorage contention issues

1 participant