From ab94794b5a354d4805f6d1875c3948fb78082636 Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Mon, 23 Jul 2018 16:13:37 -0700 Subject: [PATCH] Segment locally stored data by access token to allow multiple embeds from same source url with different tokens. --- src/util/mapbox.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/util/mapbox.js b/src/util/mapbox.js index ca8a0eef687..bbda96b292c 100644 --- a/src/util/mapbox.js +++ b/src/util/mapbox.js @@ -151,12 +151,18 @@ class TurnstileEvent { if (this.pendingRequest || this.queue.length === 0) { return; } - let dueForEvent = false; + const storageKey = `${turnstileEventStorageKey}:${config.ACCESS_TOKEN || ''}`; + let dueForEvent = (this.eventData.accessToken !== config.ACCESS_TOKEN); + + //Reset event data cache if the access token changed. + if (dueForEvent) { + this.eventData.anonId = this.eventData.lastSuccess = null; + } if (!this.eventData.anonId || !this.eventData.lastSuccess && isLocalStorageAvailable) { //Retrieve cached data try { - const data = window.localStorage.getItem(turnstileEventStorageKey); + const data = window.localStorage.getItem(storageKey); if (data) { this.eventData = JSON.parse(data); } @@ -179,8 +185,6 @@ class TurnstileEvent { dueForEvent = dueForEvent || daysElapsed >= 1 || daysElapsed < 0 || lastUpdate.getDate() !== nextDate.getDate(); } - dueForEvent = dueForEvent || (this.eventData.accessToken !== config.ACCESS_TOKEN); - if (!dueForEvent) { return this.processRequests(); } @@ -210,7 +214,7 @@ class TurnstileEvent { this.eventData.accessToken = config.ACCESS_TOKEN; if (isLocalStorageAvailable) { try { - window.localStorage.setItem(turnstileEventStorageKey, this.eventData); + window.localStorage.setItem(storageKey, JSON.stringify(this.eventData)); } catch (e) { warnOnce('Unable to write to LocalStorage'); }