preload is terrible..

This commit is contained in:
Niki Wix Skaarup 2025-04-05 23:55:02 +02:00
parent a127796498
commit 77c6e7e8c5
Signed by: nikiskaarup
GPG key ID: FC2F1B116F6E788C
2 changed files with 17 additions and 4 deletions

File diff suppressed because one or more lines are too long

View file

@ -19,6 +19,19 @@ if (!globalThis.loginTokens) {
const authCookie = 'pt-auth'; const authCookie = 'pt-auth';
let entriesCache = '';
let entriesCacheTime = 0;
const entriesCacheTimeLimit = 1000 * 15;
async function getEntriesCached() {
if (entriesCacheTime + entriesCacheTimeLimit < Date.now()) {
const data = await ptApi.query();
entriesCache = JSON.stringify(data);
entriesCacheTime = Date.now();
}
return entriesCache;
}
Bun.serve({ Bun.serve({
routes: { routes: {
'/': homepage, '/': homepage,
@ -36,9 +49,7 @@ Bun.serve({
async GET(req) { async GET(req) {
if (!isLoggedIn(req)) return unauthorizedResp(); if (!isLoggedIn(req)) return unauthorizedResp();
const data = await ptApi.query(); return new Response(await getEntriesCached(), {
const body = JSON.stringify(data);
return new Response(body, {
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
@ -89,6 +100,9 @@ Bun.serve({
globalThis.loginTokens.add(token); globalThis.loginTokens.add(token);
if (entriesCacheTime + entriesCacheTimeLimit < Date.now()) {
setTimeout(getEntriesCached, 1);
}
return new Response('Login successful', { headers }); return new Response('Login successful', { headers });
} }