Skip to content

Commit

Permalink
Implement session caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Oct 3, 2020
1 parent 0b13139 commit 3d1dc08
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
12 changes: 10 additions & 2 deletions lib/session.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries';
import { getClientInfo } from 'lib/request';
import { uuid, isValidUuid } from 'lib/crypto';
import { uuid, isValidUuid, parseToken } from 'lib/crypto';

export async function getSession(req) {
const { payload } = req.body;
Expand All @@ -9,7 +9,15 @@ export async function getSession(req) {
throw new Error('Invalid request');
}

const { website: website_uuid, hostname, screen, language } = payload;
const { website: website_uuid, hostname, screen, language, cache } = payload;

if (cache) {
const result = await parseToken(cache);

if (result) {
return result;
}
}

if (!isValidUuid(website_uuid)) {
throw new Error(`Invalid website: ${website_uuid}`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "umami",
"version": "0.72.0",
"version": "0.73.0",
"description": "A simple, fast, website analytics alternative to Google Analytics. ",
"author": "Mike Cao <[email protected]>",
"license": "MIT",
Expand Down
7 changes: 5 additions & 2 deletions pages/api/collect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import isBot from 'isbot-fast';
import { savePageView, saveEvent } from 'lib/queries';
import { useCors, useSession } from 'lib/middleware';
import { ok, badRequest } from 'lib/response';
import isBot from 'isbot-fast';
import { createToken } from 'lib/crypto';

export default async (req, res) => {
if (isBot(req.headers['user-agent'])) {
Expand All @@ -28,5 +29,7 @@ export default async (req, res) => {
return badRequest(res);
}

return ok(res);
const token = await createToken({ website_id, session_id });

return ok(res, token);
};
21 changes: 15 additions & 6 deletions tracker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { removeTrailingSlash } from '../lib/url';
screen: { width, height },
navigator: { language },
location: { hostname, pathname, search },
sessionStorage,
document,
history,
} = window;
Expand All @@ -16,7 +17,8 @@ import { removeTrailingSlash } from '../lib/url';
const website = attr('data-website-id');
const hostUrl = attr('data-host-url');
const autoTrack = attr('data-auto-track') !== 'false';
const dnt = attr('data-do-not-track') === 'true';
const dnt = attr('data-do-not-track');
const useCache = attr('data-cache');

if (!script || (dnt && doNotTrack())) return;

Expand All @@ -37,19 +39,22 @@ import { removeTrailingSlash } from '../lib/url';

req.onreadystatechange = () => {
if (req.readyState === 4) {
callback && callback();
callback && callback(req.response);
}
};

req.send(JSON.stringify(data));
};

const collect = (type, params, uuid) => {
const key = 'umami.cache';

const payload = {
website: uuid,
hostname,
screen,
language,
cache: useCache && sessionStorage.getItem(key),
};

if (params) {
Expand All @@ -58,10 +63,14 @@ import { removeTrailingSlash } from '../lib/url';
});
}

return post(`${root}/api/collect`, {
type,
payload,
});
return post(
`${root}/api/collect`,
{
type,
payload,
},
res => useCache && sessionStorage.setItem(key, res),
);
};

const trackView = (url = currentUrl, referrer = currentRef, uuid = website) =>
Expand Down

0 comments on commit 3d1dc08

Please sign in to comment.