All Web- and ServiceWorkers reliably destroyed by new `cognito-identity-js`
See original GitHub issueDescribe the bug
With the introduction of isomorphic-unfetch
to amazon-cognito-identity-js/src/Client.js
at https://github.com/aws-amplify/amplify-js/blob/7cbed47cb2bbbc3a94a6fcae87efee80a5542f2e/packages/amazon-cognito-identity-js/src/Client.js#L1 any Service Worker or Web Worker making API calls will crash with a window not found
. Moreover, the inclusion of file in a JS bundle instantly destroys the browser’s Worker environment, with other fatal exceptions soon to follow.
This is because isomorphic-unfetch
depends on unfetch
, which fails to check for self
(i.e., a Worker environment), and, thereby, replaces the Worker environment’s built-in fetch
implementation with a fetch polyfill that, incorrectly, assumes window
is present.
https://github.com/developit/unfetch/issues/104
To fully appreciate the severity of this bug, consider that that the standardized worker implementation of the fetch
spec (in Chrome it’s >> 100,000 lines, and includes proxying, cacheing, etc) is replaced by a 500 line polyfill the moment the worker loads the Amplify JS bundle, and executes import "isomorphic-unfetch"
.
Unfortunately, theunfetch
project is not actively maintained, with a straightforward PR to fix the problem being outstanding almost 2 years:
https://github.com/developit/unfetch/pull/109
The problem CAN be fixed without impacting any other Amplify code by either:
1. replacing unfetch
with a fetch
polyfill that correctly detects all environments,
OR
2. checking for self
on the 1st line of Client.js
, and, if a Worker is detected, not importing isomorphic-fetch
and overwriting the Worker global namespace
To Reproduce
Based on the self-evident nature of the problem and its documentation in the isomorphic-unfetch
/unfetch
repo, reproducing the behavior in a Worker using Amplify should not be necessary… but, if our process nonetheless requires reproducing, do this:
From within a Web or Service Worker, call any method in @aws-amplify/auth
(which will, in turn invoke amazon-cognito-identity-js/src/Client.js
) or any other Amplify function dependent on @aws-amplify/auth
.
The /amazon-cognito-identity-js/__tests__/
does not currently consider workers (nor SSR). Similarly, there is no testing for authentication in workers in core… all of which explains why this problem wasn’t previously identified.
To configure a worker to use Auth, you’ll need to provide an implementation of a the ICognitoStorage
interface with your Config with something like:
Auth.configure({
storage: AuthStorage,
});
The custom storage interface is required because workers don’t support LocalStorage, Amplify’s default store. Numerous examples for memory and IndexedDB stores can be found with a Google search.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
For reference, it looks like https://github.com/supabase/postgrest-js/pull/109 moved to use cross-fetch which doesn’t appear to have the issue when run within workers so using that over the current solution is one option.
This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server
*-help
channels or Discussions for those types of questions.