Pre-fetch the config.json to improve startup time

This commit is contained in:
Quentin Gliech 2024-11-15 13:25:33 +01:00
parent 9650ca9687
commit 2e172aa3be
No known key found for this signature in database
GPG Key ID: 22D62B84552719FC
3 changed files with 8 additions and 11 deletions

View File

@ -8,7 +8,7 @@ server {
location / { location / {
try_files $uri $uri/ /index.html; try_files $uri $uri/ /index.html;
add_header Cache-Control "no-store, no-cache, must-revalidate"; add_header Cache-Control "public, max-age=30, stale-while-revalidate=30";
} }
# assets can be cached because they have hashed filenames # assets can be cached because they have hashed filenames

View File

@ -3,6 +3,7 @@
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="favicon.png" /> <link rel="icon" type="image/svg+xml" href="favicon.png" />
<link rel="preload" href="/config.json" as="fetch" />
<meta <meta
name="viewport" name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"

View File

@ -28,11 +28,11 @@ export class Config {
const internalInstance = new Config(); const internalInstance = new Config();
Config.internalInstance = internalInstance; Config.internalInstance = internalInstance;
Config.internalInstance.initPromise = downloadConfig( Config.internalInstance.initPromise = downloadConfig("/config.json").then(
"../config.json", (config) => {
).then((config) => {
internalInstance.config = merge({}, DEFAULT_CONFIG, config); internalInstance.config = merge({}, DEFAULT_CONFIG, config);
}); },
);
} }
return Config.internalInstance.initPromise; return Config.internalInstance.initPromise;
} }
@ -74,11 +74,7 @@ async function downloadConfig(
configJsonFilename: string, configJsonFilename: string,
): Promise<ConfigOptions> { ): Promise<ConfigOptions> {
const url = new URL(configJsonFilename, window.location.href); const url = new URL(configJsonFilename, window.location.href);
url.searchParams.set("cachebuster", Date.now().toString()); const res = await fetch(url);
const res = await fetch(url, {
cache: "no-cache",
method: "GET",
});
if (!res.ok || res.status === 404 || res.status === 0) { if (!res.ok || res.status === 404 || res.status === 0) {
// Lack of a config isn't an error, we should just use the defaults. // Lack of a config isn't an error, we should just use the defaults.