preload is terrible..
This commit is contained in:
parent
a127796498
commit
77c6e7e8c5
2 changed files with 17 additions and 4 deletions
File diff suppressed because one or more lines are too long
|
@ -19,6 +19,19 @@ if (!globalThis.loginTokens) {
|
|||
|
||||
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({
|
||||
routes: {
|
||||
'/': homepage,
|
||||
|
@ -36,9 +49,7 @@ Bun.serve({
|
|||
async GET(req) {
|
||||
if (!isLoggedIn(req)) return unauthorizedResp();
|
||||
|
||||
const data = await ptApi.query();
|
||||
const body = JSON.stringify(data);
|
||||
return new Response(body, {
|
||||
return new Response(await getEntriesCached(), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
@ -89,6 +100,9 @@ Bun.serve({
|
|||
|
||||
globalThis.loginTokens.add(token);
|
||||
|
||||
if (entriesCacheTime + entriesCacheTimeLimit < Date.now()) {
|
||||
setTimeout(getEntriesCached, 1);
|
||||
}
|
||||
return new Response('Login successful', { headers });
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue