amplitude / amplitude/Amplitude-TypeScript

Handling localStorage Access Restrictions in Iframe for Amplitude

Open
#651 0 comments 5 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
180
Forks
68
Avg merge
3d 1h
Merged PRs (30d)
29

Description

## Expected Behavior
The Amplitude JavaScript SDK should handle environments where localStorage access is restricted (such as within certain iframes) without causing uncaught exceptions or crashes in the application.

## Current Behavior
When the Amplitude SDK is used in an environment where localStorage is restricted (like in a cross-origin iframe), it attempts to access localStorage and triggers a SecurityError. This issue causes the application to crash or behave unexpectedly due to unhandled exceptions.
Error path: node_modules/@amplitude/analytics-browser/lib/esm/storage/local-storage.js

## Possible Solution
Implement a check within the SDK to gracefully handle scenarios where localStorage access is not available. This could include try-catch blocks around localStorage access or alternative storage mechanisms when localStorage is not accessible.

## Steps to Reproduce
1. Embed a webpage in a cross-origin iframe.
2. Integrate the Amplitude into the webpage.
3. Load the webpage within the iframe and observe the JavaScript console for errors related to localStorage access.
4. Notice that the application crashes or behaves unexpectedly.

## Environment
- JS SDK Version: 2.1.2
- Installation Method: Yarn
- Browser and Version: Chrome - Version 119.0.6045.199

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.