diff --git a/lib/session.js b/lib/session.js index 0488b730..3d2fc7bc 100644 --- a/lib/session.js +++ b/lib/session.js @@ -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}`); diff --git a/package.json b/package.json index df40306e..54512997 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", diff --git a/pages/api/collect.js b/pages/api/collect.js index 399e88e9..e6b3e0ca 100644 --- a/pages/api/collect.js +++ b/pages/api/collect.js @@ -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); }; diff --git a/tracker/index.js b/tracker/index.js index 12119298..154e0034 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -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) =>