Skip to content

Commit 2284b8e

Browse files
authored
Merge pull request tiagozip#135 from sersoft-gmbh/bugfix/asset-cache
2 parents 0f1eaad + 6e173b7 commit 2284b8e

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

standalone/src/assets.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,29 @@ const dataDir = process.env.DATA_PATH || "./.data";
1818
const updateCache = async () => {
1919
if (process.env.ENABLE_ASSETS_SERVER !== "true") return;
2020

21+
const cacheConfigPath = path.join(dataDir, "assets-cache.json");
2122
let cacheConfig = {};
2223

2324
try {
24-
cacheConfig = JSON.parse(await fs.readFile("./assets-cache.json", "utf-8"));
25+
cacheConfig = JSON.parse(await fs.readFile(cacheConfigPath, "utf-8"));
2526
} catch {}
2627

2728
const lastUpdate = cacheConfig.lastUpdate || 0;
2829
const currentTime = Date.now();
2930
const updateInterval = 1000 * 60 * 60 * 24; // 1 day
30-
31-
if (!(currentTime - lastUpdate > updateInterval)) return;
32-
33-
const CACHE_HOST = process.env.CACHE_HOST || "https://cdn.jsdelivr.net";
31+
const intervalExceeded = currentTime - lastUpdate > updateInterval;
3432

3533
const WIDGET_VERSION = process.env.WIDGET_VERSION || "latest";
3634
const WASM_VERSION = process.env.WASM_VERSION || "latest";
3735

36+
if (!cacheConfig.versions) cacheConfig.versions = {};
37+
const versionsChanged = cacheConfig.versions.widget !== WIDGET_VERSION
38+
|| cacheConfig.versions.wasm !== WASM_VERSION;
39+
40+
if (!intervalExceeded && !versionsChanged) return;
41+
42+
const CACHE_HOST = process.env.CACHE_HOST || "https://cdn.jsdelivr.net";
43+
3844
try {
3945
const [widgetSource, floatingSource, wasmSource, wasmLoaderSource] =
4046
await Promise.all([
@@ -53,10 +59,9 @@ const updateCache = async () => {
5359
]);
5460

5561
cacheConfig.lastUpdate = currentTime;
56-
await fs.writeFile(
57-
path.join(dataDir, "assets-cache.json"),
58-
JSON.stringify(cacheConfig),
59-
);
62+
cacheConfig.versions.widget = WIDGET_VERSION;
63+
cacheConfig.versions.wasm = WASM_VERSION;
64+
await fs.writeFile(cacheConfigPath, JSON.stringify(cacheConfig));
6065

6166
await fs.writeFile(path.join(dataDir, "assets-widget.js"), widgetSource);
6267
await fs.writeFile(
@@ -77,7 +82,7 @@ const updateCache = async () => {
7782
};
7883

7984
updateCache();
80-
setInterval(updateCache, 1000 * 60 * 60);
85+
setInterval(updateCache, 1000 * 60 * 60); // 1 hour
8186

8287
export const assetsServer = new Elysia({
8388
prefix: "/assets",

0 commit comments

Comments
 (0)