Skip to content

Commit

Permalink
Merge pull request #335 from MindscapeHQ/revert-session-storage-to-local
Browse files Browse the repository at this point in the history
Revert sessionstorage to localstoage
  • Loading branch information
BenjaminHarding authored Jul 24, 2019
2 parents 6d960c2 + 9f7fbb9 commit 9565937
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* v2.17.1
- Fix regression and use localStorage to persist RUM sessions instead of sessionStorage

* v2.17.0
- Add inital support for users to set the client IP address

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "raygun4js",
"version": "2.17.0",
"version": "2.17.1",
"homepage": "http://raygun.io",
"authors": [
"Mindscape <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"title": "Raygun4js",
"description": "Raygun.io plugin for JavaScript",
"version": "2.17.0",
"version": "2.17.1",
"homepage": "https://github.com/MindscapeHQ/raygun4js",
"author": {
"name": "MindscapeHQ",
Expand Down
2 changes: 1 addition & 1 deletion raygun4js.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>raygun4js</id>
<version>2.17.0</version>
<version>2.17.1</version>
<title>Raygun4js</title>
<authors>Mindscape Limited</authors>
<owners>Mindscape Limited</owners>
Expand Down
20 changes: 14 additions & 6 deletions src/raygun.rum.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,36 +923,44 @@ var raygunRumFactory = function(window, $, Raygun) {
var lastActivityTimestamp = new Date().toISOString();
var updatedValue = 'id|' + value + '&timestamp|' + lastActivityTimestamp;

if(Raygun.Utilities.sessionStorageAvailable()) {
sessionStorage.setItem(self.cookieName, updatedValue);
if(Raygun.Utilities.localStorageAvailable()) {
localStorage.setItem(self.cookieName, updatedValue);
} else {
Raygun.Utilities.createCookie(self.cookieName, updatedValue, null, self.setCookieAsSecure);
}
}

function getFromStorage() {
/**
* Attempt to get the value from session storage,
* Attempt to get the value from local storage,
* If that doesn't contain a value then try from a cookie as previous versions saved it here
*/
var value;

if(Raygun.Utilities.localStorageAvailable()) {
value = localStorage.getItem(self.cookieName);
if(value !== null) {
return value;
}
}

if(Raygun.Utilities.sessionStorageAvailable()) {
value = sessionStorage.getItem(self.cookieName);
if(value !== null) {
saveToStorage(value);
return value;
}
}

value = Raygun.Utilities.readCookie(self.cookieName);

/**
* If there was a cookie and sessionStorage is avaliable then
* If there was a cookie and localStorage is avaliable then
* clear the cookie as sessionStorage will be the storage mechanism going forward
*/
if(value !== null && Raygun.Utilities.sessionStorageAvailable()) {
if(value !== null && Raygun.Utilities.localStorageAvailable()) {
Raygun.Utilities.clearCookie(self.cookieName);
sessionStorage.setItem(self.cookieName, value);
localStorage.setItem(self.cookieName, value);
}

return value;
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
},
getSessionStorageValue: function(key) {
return browser.execute(function (name) {
return sessionStorage.getItem(name);
return localStorage.getItem(name);
}, key).value;
},
setCookieValue: function(key, value) {
Expand Down
20 changes: 10 additions & 10 deletions tests/specs/sessions/realUserMonitoring.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ describe("RUM Session Tracking", function() {

afterEach(function() {
browser.execute(function() {
sessionStorage.clear();
localStorage.clear();
});
});

it("persists a session id into storage when one doesn't exist", function() {
browser.url("http://localhost:4567/fixtures/sessions/rumSession.html");

var result = common.getSessionStorageValue("raygun4js-sid");
var result = common.getLocalStorageValue("raygun4js-sid");
expect(result).not.toBe(null);
});

Expand All @@ -31,12 +31,12 @@ describe("RUM Session Tracking", function() {
var sessionValue = 'abc123';

var sessionString = 'id|' + sessionValue + '&timestamp|' + timestamp;
sessionStorage.setItem('raygun4js-sid', sessionString);
localStorage.setItem('raygun4js-sid', sessionString);
}, oneMinuteAgoTimestamp);

browser.url("http://localhost:4567/fixtures/sessions/rumSession.html");

var result = common.getSessionStorageValue("raygun4js-sid")
var result = common.getLocalStorageValue("raygun4js-sid")

const set = result.split(/[|&]/);

Expand All @@ -49,7 +49,7 @@ describe("RUM Session Tracking", function() {
expect(newTimestampIsGreater).toBe(true);
});

it("retrieves session id from a cookie and sets it in sessionStorage", function() {
it("retrieves session id from a cookie and sets it in localStorage", function() {
var sessionValue = 'cookieId';
var timestamp = new Date(new Date() - 60000).toISOString();
var cookieValue = 'id|' + sessionValue + '&timestamp|' + timestamp;
Expand All @@ -58,7 +58,7 @@ describe("RUM Session Tracking", function() {

browser.url("http://localhost:4567/fixtures/sessions/rumSession.html");

var result = common.getSessionStorageValue("raygun4js-sid")
var result = common.getLocalStorageValue("raygun4js-sid")

const set = result.split(/[|&]/);

Expand All @@ -75,25 +75,25 @@ describe("RUM Session Tracking", function() {
var sessionValue = 'expiredId';
var oneHourAgoTimestamp = new Date(new Date() - 60 * 60000).toISOString();
var sessionString = 'id|' + sessionValue + '&timestamp|' + oneHourAgoTimestamp;
sessionStorage.setItem('raygun4js-sid', sessionString);
localStorage.setItem('raygun4js-sid', sessionString);
});

browser.url("http://localhost:4567/fixtures/sessions/rumSession.html");

var result = common.getSessionStorageValue("raygun4js-sid")
var result = common.getLocalStorageValue("raygun4js-sid")

expect(result.indexOf('id|expiredId')).toBe(-1);
expect(result.split('&')[0]).not.toBe('id|expiredId');
});

it("creates a new session id if value stored is null", function() {
browser.execute(function() {
sessionStorage.setItem('raygun4js-sid', null);
localStorage.setItem('raygun4js-sid', null);
});

browser.url("http://localhost:4567/fixtures/sessions/rumSession.html");

var result = common.getSessionStorageValue("raygun4js-sid")
var result = common.getLocalStorageValue("raygun4js-sid")
expect(result).not.toBe(null);
});
});
Expand Down

0 comments on commit 9565937

Please sign in to comment.