Skip to content

Commit

Permalink
Issue-58: Resilient to localStorage is null (#59)
Browse files Browse the repository at this point in the history
* Issue-58: Resilient to localStorage is null.
  • Loading branch information
naveenvalecha authored Feb 19, 2024
1 parent ea00833 commit 44bd0f8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
15 changes: 15 additions & 0 deletions cypress/e2e/functional/localStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Test for Convivial Profiler with no local storage.

describe('No Local Storage: Test 01', () => {
it('1. Check no error is thrown if the localstorage is disabled.'
, () => {
cy.disableLocalStorage();
// Open the default page.
cy.visit(Cypress.env('baseUrl'));
cy.window()
.its('console')
.then((console) => {
cy.stub(console, 'error').throws('Console error')
})
})
});
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/convivial-profiler-init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function (window, ConvivialProfiler, config) {
window.convivialProfiler = new ConvivialProfiler(config.config, config.site);
window.convivialProfiler = new ConvivialProfiler(config.config, config.site, config.license_key);
window.convivialProfiler.collect();
})(window, window.ConvivialProfiler.default, drupalSettings.convivialProfiler);
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { accumulation, dimension, extreme_geoip, language_simple, language_full, map, pageview, searchquery, store, unstore_value, temp } from "./modules/processor"
import { bestpick, copy, datalayer_event, flag, formfiller, formtracker, officehours, range, remove, season, set, threshold, top, unset } from "./modules/destination"
import { acceptlang, cookie, get, meta, query, time, httpuseragent } from "./modules/source"
import { getTime, getClientId } from "./lib/utility"
import { getTime, getClientId, isLocalStorageAvailable } from "./lib/utility"

class ConvivialProfiler {

Expand All @@ -18,6 +18,9 @@ class ConvivialProfiler {
this.siteId = siteId;
this.licenseKey = licenseKey;
this.clientId = clientId || getClientId();
if (!isLocalStorageAvailable()) {
return;
}
this.storage = this._loadStorage();
this.storage.temp = {};

Expand Down Expand Up @@ -63,6 +66,9 @@ class ConvivialProfiler {
}

collect() {
if (!isLocalStorageAvailable()) {
return;
}
var now = getTime();
Object.keys(this.config.profilers).forEach(name => {
var profiler = this.config.profilers[name];
Expand Down
17 changes: 16 additions & 1 deletion src/lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@ function getClientId() {
}
return value;
};
function isLocalStorageAvailable() {
try {
if (typeof localStorage === 'undefined') {
return false;
}
var test = 'test';
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
console.log('Convivial Profiler will not work because the browser local storage is not enabled or accessible.');
return false;
}
}
export {
getTime,
getCookie,
setCookie,
getClientId
getClientId,
isLocalStorageAvailable
}

0 comments on commit 44bd0f8

Please sign in to comment.