[FIX] website_event_track: crash in SW's fetch listener on Safari 17.0+

Before this commit, when browsing the "Events" part of website on Safari
(both macOS and iOS/ipadOS), the page could be loaded only once and
returns a "FetchEvent.respondWith received an error: NotSupportedError:
The operation is not supported." error message on afterward, completely
blocking access to everything under the `/event` path.

This error is actually thrown from the ServiceWorker's `fetch` event's
listener, and more specifically, from the storage availablity check.

Since Safari 17.0, the Storage API - and in this case its `estimate()`
function -  has been enabled in WebKit's builds for Apple platforms (see
WebKit PR [1]). But even if this feature should be available in Web
Workers (cf. MDN [2] and the spec [3]), it returns a NotSupportedError
error when called from the ServiceWorker on Safari 17.0+ (but works fine
in the global scope).

This commit works around that issue by wrapping this call in a
try/catch, acting as if not supported when an error happens.

Steps to reproduce (on Safari iOS):
- Install website_event_track module
- Navigate to the `/event` page
- Reload the page
=> Browser level error page "FetchEvent.respondWith received an
error..."

Note: due to the browser's engine restriction on iOS/ipadOS, this issue
also affects all browsers on these platforms.

[1]: https://github.com/WebKit/WebKit/pull/10973
[2]: https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/estimate
[3]: https://storage.spec.whatwg.org/#ref-for-dom-storagemanager-estimate

opw-3553880
opw-3570730
opw-3610167
opw-3629039
opw-3547759

closes odoo/odoo#146190

X-original-commit: 45020635a81ce53ee0769ca54ac733528e4110b1
Signed-off-by: Romeo Fragomeli (rfr) <rfr@odoo.com>
This commit is contained in:
Pierre Paridans
2023-12-14 06:56:23 +00:00
parent 3890a4a540
commit 7a5cabe889
@@ -61,8 +61,13 @@ const isCacheFull = async () => {
if (!("storage" in navigator && "estimate" in navigator.storage)) {
return false;
}
const { usage, quota } = await navigator.storage.estimate();
return usage / quota > MAX_CACHE_QUOTA || usage > MAX_CACHE_SIZE;
try {
const { usage, quota } = await navigator.storage.estimate();
return usage / quota > MAX_CACHE_QUOTA || usage > MAX_CACHE_SIZE;
} catch (error) {
console.error(`call to storage.estimate failed`, error);
return false;
}
};
/**