AxaFrance / oidc-client

Light, Secure, Pure Javascript OIDC (Open ID Connect) Client. We provide also a REACT wrapper (compatible NextJS, etc.).
MIT License
570 stars 152 forks source link

Too many logs and errors for OidcKeepAliveServiceWorker fetches #1350

Open wermanoid opened 2 months ago

wermanoid commented 2 months ago

Issue and Steps to Reproduce

Too much failed requests when trying to fetch OidcKeepAliveServiceWorker.json, that leads to bunch of errors in network tab and console logs. Also process is random and sometimes it could stay clean for some time.

To Reproduce

You need just run OIDC client with tokens refresh and wait until OidcKeepAliveServiceWorker will start randomly write error logs

Versions

Latest

Screenshots

image image

Expected

Library should not spam error logs into console, especially when error is random and does not impact on app. This is confusing

Actual

Too much errors in browser.

Additional Details

In OidcServiceWorker.ts there is a function keepAlive and it has this randomiser:

for (let i = 0; i < minSleepSeconds; i++) {
    await sleep(1000 + Math.floor(Math.random() * 1000));
    const cache = await caches.open('oidc_dummy_cache');
    await cache.put(event.request, response.clone());
}

Problem is next:

And it is weird, that random function over 150 times resolves into same values (like we have always sleep(2000) result). Here, my assumption, this also might be related to sleep function implementation. As it triggers setTimeout, there is no guarantee that this timeout will be executed immediately, as other functions in app/service worker could also inject some functions into browser call stack. So that leads to longer delays.

This leads to situation when network tab and console are overwhelmed with errors, as this timer:

Proposals

Not sure what could be better, but some ideas:

wermanoid commented 2 months ago

Probably https://github.com/AxaFrance/oidc-client/issues/1303 has same source