mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-30 16:57:44 +01:00
Implement session caching.
This commit is contained in:
parent
0b131392fc
commit
3d1dc08491
@ -1,6 +1,6 @@
|
|||||||
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries';
|
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries';
|
||||||
import { getClientInfo } from 'lib/request';
|
import { getClientInfo } from 'lib/request';
|
||||||
import { uuid, isValidUuid } from 'lib/crypto';
|
import { uuid, isValidUuid, parseToken } from 'lib/crypto';
|
||||||
|
|
||||||
export async function getSession(req) {
|
export async function getSession(req) {
|
||||||
const { payload } = req.body;
|
const { payload } = req.body;
|
||||||
@ -9,7 +9,15 @@ export async function getSession(req) {
|
|||||||
throw new Error('Invalid request');
|
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)) {
|
if (!isValidUuid(website_uuid)) {
|
||||||
throw new Error(`Invalid website: ${website_uuid}`);
|
throw new Error(`Invalid website: ${website_uuid}`);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "umami",
|
"name": "umami",
|
||||||
"version": "0.72.0",
|
"version": "0.73.0",
|
||||||
"description": "A simple, fast, website analytics alternative to Google Analytics. ",
|
"description": "A simple, fast, website analytics alternative to Google Analytics. ",
|
||||||
"author": "Mike Cao <mike@mikecao.com>",
|
"author": "Mike Cao <mike@mikecao.com>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import isBot from 'isbot-fast';
|
||||||
import { savePageView, saveEvent } from 'lib/queries';
|
import { savePageView, saveEvent } from 'lib/queries';
|
||||||
import { useCors, useSession } from 'lib/middleware';
|
import { useCors, useSession } from 'lib/middleware';
|
||||||
import { ok, badRequest } from 'lib/response';
|
import { ok, badRequest } from 'lib/response';
|
||||||
import isBot from 'isbot-fast';
|
import { createToken } from 'lib/crypto';
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
if (isBot(req.headers['user-agent'])) {
|
if (isBot(req.headers['user-agent'])) {
|
||||||
@ -28,5 +29,7 @@ export default async (req, res) => {
|
|||||||
return badRequest(res);
|
return badRequest(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ok(res);
|
const token = await createToken({ website_id, session_id });
|
||||||
|
|
||||||
|
return ok(res, token);
|
||||||
};
|
};
|
||||||
|
@ -6,6 +6,7 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
screen: { width, height },
|
screen: { width, height },
|
||||||
navigator: { language },
|
navigator: { language },
|
||||||
location: { hostname, pathname, search },
|
location: { hostname, pathname, search },
|
||||||
|
sessionStorage,
|
||||||
document,
|
document,
|
||||||
history,
|
history,
|
||||||
} = window;
|
} = window;
|
||||||
@ -16,7 +17,8 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
const website = attr('data-website-id');
|
const website = attr('data-website-id');
|
||||||
const hostUrl = attr('data-host-url');
|
const hostUrl = attr('data-host-url');
|
||||||
const autoTrack = attr('data-auto-track') !== 'false';
|
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;
|
if (!script || (dnt && doNotTrack())) return;
|
||||||
|
|
||||||
@ -37,7 +39,7 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
|
|
||||||
req.onreadystatechange = () => {
|
req.onreadystatechange = () => {
|
||||||
if (req.readyState === 4) {
|
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 collect = (type, params, uuid) => {
|
||||||
|
const key = 'umami.cache';
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
website: uuid,
|
website: uuid,
|
||||||
hostname,
|
hostname,
|
||||||
screen,
|
screen,
|
||||||
language,
|
language,
|
||||||
|
cache: useCache && sessionStorage.getItem(key),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (params) {
|
if (params) {
|
||||||
@ -58,10 +63,14 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return post(`${root}/api/collect`, {
|
return post(
|
||||||
type,
|
`${root}/api/collect`,
|
||||||
payload,
|
{
|
||||||
});
|
type,
|
||||||
|
payload,
|
||||||
|
},
|
||||||
|
res => useCache && sessionStorage.setItem(key, res),
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) =>
|
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) =>
|
||||||
|
Loading…
Reference in New Issue
Block a user