From dc95d43efbfbbe646e41ab5b7d4f5e2431f39c91 Mon Sep 17 00:00:00 2001 From: Pierre Paridans Date: Thu, 24 Sep 2020 12:55:54 +0000 Subject: [PATCH] [FIX] website_event_track: only cache local & cdn based urls This commit allows to filter out the requests to local-ish domains (cf. the current host and an optionally configured CDN for the current website) and preventing from cluttering the cache used by the ServiceWorker with tracking files, scripts or other non-necessary assets. This allows to even further reduce the possible "opaque" requests from being stored in the cache (as they use way more space than regular/basic responses). X-original-commit: 052bc7665e89426fb51928e43bd4a15dbf6cb705 --- addons/website_event_track/controllers/webmanifest.py | 7 ++++++- .../static/src/js/service_worker.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/addons/website_event_track/controllers/webmanifest.py b/addons/website_event_track/controllers/webmanifest.py index 9f4967ed70d..627c2581577 100644 --- a/addons/website_event_track/controllers/webmanifest.py +++ b/addons/website_event_track/controllers/webmanifest.py @@ -48,8 +48,13 @@ class TrackManifest(http.Controller): """ Returns a ServiceWorker javascript file scoped for website_event """ sw_file = get_module_resource('website_event_track', 'static/src/js/service_worker.js') - with open(sw_file, 'rb') as fp: + with open(sw_file, 'r') as fp: body = fp.read() + js_cdn_url = 'undefined' + if request.website.cdn_activated: + cdn_url = request.website.cdn_url.replace('"','%22').replace('\x5c','%5C') + js_cdn_url = '"%s"' % cdn_url + body = body.replace('__ODOO_CDN_URL__', js_cdn_url) response = request.make_response(body, [ ('Content-Type', 'text/javascript'), ('Service-Worker-Allowed', url_for('/event')), diff --git a/addons/website_event_track/static/src/js/service_worker.js b/addons/website_event_track/static/src/js/service_worker.js index c6b6800aacf..41ce7e54297 100644 --- a/addons/website_event_track/static/src/js/service_worker.js +++ b/addons/website_event_track/static/src/js/service_worker.js @@ -5,6 +5,7 @@ const SYNCABLE_ROUTES = ["/event/track/toggle_reminder"]; const CACHABLE_ROUTES = ["/web/webclient/version_info"]; const MAX_CACHE_SIZE = 512 * 1024 * 1024; // 500 MB const MAX_CACHE_QUOTA = 0.5; +const CDN_URL = __ODOO_CDN_URL__; // {string|undefined} the cdn_url configured for the website if activated const { Store, set, get, del } = idbKeyval; const pendingRequestsQueueName = `${PREFIX}-pending-requests`; @@ -12,6 +13,8 @@ const cacheName = `${PREFIX}-cache`; const syncStore = new Store(`${PREFIX}-sync-db`, `${PREFIX}-sync-store`); const cacheStore = new Store(`${PREFIX}-cache-db`, `${PREFIX}-cache-store`); const offlineRoute = `${self.registration.scope}/offline`; +const scopeURL = new URL(self.registration.scope); +const cdnURL = CDN_URL ? (CDN_URL.startsWith("http") ? new URL(CDN_URL) : new URL(`http:${CDN_URL}`)) : undefined; /** * @@ -147,6 +150,13 @@ const buildEmptyResponse = () => new Response(JSON.stringify({ jsonrpc: "2.0", i * @returns {Promise} */ const cacheRequest = async (request, response) => { + // only attempts to cache local or cdn delivered urls + const url = new URL(request.url); + if (url.hostname !== scopeURL.hostname && (!cdnURL || url.hostname !== cdnURL.hostname)) { + console.error(`ignoring cache for ${request.url} => ${url.hostname}, local: ${scopeURL.hostname}, cdn: ${cdnURL ? cdnURL.hostname : cdnURL}`); + return; + } + // don't even attempt to cache: // - error pages (why cache that?) // - non-"basic" response types, which include tracker 1-time opaque requests