Enabling caching in tracker script.

This commit is contained in:
Mike Cao 2022-03-18 22:26:23 -07:00
parent 777dfeac0e
commit 7cf0927741
2 changed files with 5 additions and 6 deletions

View File

@ -9,7 +9,8 @@ export async function getSession(req) {
throw new Error('Invalid request'); throw new Error('Invalid request');
} }
const { website: website_uuid, hostname, screen, language, cache } = payload; const { website: website_uuid, hostname, screen, language } = payload;
const cache = req.headers['x-umami-cache'];
if (cache) { if (cache) {
const result = await parseToken(cache); const result = await parseToken(cache);

View File

@ -7,7 +7,6 @@ import { removeTrailingSlash } from '../lib/url';
navigator: { language }, navigator: { language },
location: { hostname, pathname, search }, location: { hostname, pathname, search },
localStorage, localStorage,
sessionStorage,
document, document,
history, history,
} = window; } = window;
@ -21,14 +20,12 @@ import { removeTrailingSlash } from '../lib/url';
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'); const dnt = attr('data-do-not-track');
const useCache = attr('data-cache');
const cssEvents = attr('data-css-events') !== 'false'; const cssEvents = attr('data-css-events') !== 'false';
const domain = attr('data-domains') || ''; const domain = attr('data-domains') || '';
const domains = domain.split(',').map(n => n.trim()); const domains = domain.split(',').map(n => n.trim());
const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/; const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/;
const eventSelect = "[class*='umami--']"; const eventSelect = "[class*='umami--']";
const cacheKey = 'umami.cache';
const trackingDisabled = () => const trackingDisabled = () =>
(localStorage && localStorage.getItem('umami.disabled')) || (localStorage && localStorage.getItem('umami.disabled')) ||
@ -42,6 +39,7 @@ import { removeTrailingSlash } from '../lib/url';
const listeners = {}; const listeners = {};
let currentUrl = `${pathname}${search}`; let currentUrl = `${pathname}${search}`;
let currentRef = document.referrer; let currentRef = document.referrer;
let cache;
/* Collect metrics */ /* Collect metrics */
@ -49,6 +47,7 @@ import { removeTrailingSlash } from '../lib/url';
const req = new XMLHttpRequest(); const req = new XMLHttpRequest();
req.open('POST', url, true); req.open('POST', url, true);
req.setRequestHeader('Content-Type', 'application/json'); req.setRequestHeader('Content-Type', 'application/json');
if (cache) req.setRequestHeader('x-umami-cache', cache);
req.onreadystatechange = () => { req.onreadystatechange = () => {
if (req.readyState === 4) { if (req.readyState === 4) {
@ -64,7 +63,6 @@ import { removeTrailingSlash } from '../lib/url';
hostname, hostname,
screen, screen,
language, language,
cache: useCache && sessionStorage.getItem(cacheKey),
url: currentUrl, url: currentUrl,
}); });
@ -84,7 +82,7 @@ import { removeTrailingSlash } from '../lib/url';
type, type,
payload, payload,
}, },
res => useCache && sessionStorage.setItem(cacheKey, res), res => (cache = res),
); );
}; };