Implement session caching.

This commit is contained in:
Mike Cao 2020-10-02 20:33:46 -07:00
parent 0b131392fc
commit 3d1dc08491
4 changed files with 31 additions and 11 deletions

View File

@ -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;
@ -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}`);

View File

@ -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 <mike@mikecao.com>",
"license": "MIT",

View File

@ -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'])) {
@ -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);
};

View File

@ -6,6 +6,7 @@ import { removeTrailingSlash } from '../lib/url';
screen: { width, height },
navigator: { language },
location: { hostname, pathname, search },
sessionStorage,
document,
history,
} = window;
@ -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;
@ -37,7 +39,7 @@ import { removeTrailingSlash } from '../lib/url';
req.onreadystatechange = () => {
if (req.readyState === 4) {
callback && callback();
callback && callback(req.response);
}
};
@ -45,11 +47,14 @@ import { removeTrailingSlash } from '../lib/url';
};
const collect = (type, params, uuid) => {
const key = 'umami.cache';
const payload = {
website: uuid,
hostname,
screen,
language,
cache: useCache && sessionStorage.getItem(key),
};
if (params) {
@ -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) =>