Merge pull request #2790 from element-hq/quenting/preload-config

Pre-fetch the config.json to improve startup time
This commit is contained in:
Quentin Gliech 2024-11-15 13:55:41 +01:00 committed by GitHub
commit d77e9e8f08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 11 deletions

View File

@ -8,7 +8,7 @@ server {
location / {
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

View File

@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="favicon.png" />
<link rel="preload" href="/config.json" as="fetch" />
<meta
name="viewport"
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();
Config.internalInstance = internalInstance;
Config.internalInstance.initPromise = downloadConfig(
"../config.json",
).then((config) => {
internalInstance.config = merge({}, DEFAULT_CONFIG, config);
});
Config.internalInstance.initPromise = downloadConfig("/config.json").then(
(config) => {
internalInstance.config = merge({}, DEFAULT_CONFIG, config);
},
);
}
return Config.internalInstance.initPromise;
}
@ -74,11 +74,7 @@ async function downloadConfig(
configJsonFilename: string,
): Promise<ConfigOptions> {
const url = new URL(configJsonFilename, window.location.href);
url.searchParams.set("cachebuster", Date.now().toString());
const res = await fetch(url, {
cache: "no-cache",
method: "GET",
});
const res = await fetch(url);
if (!res.ok || res.status === 404 || res.status === 0) {
// Lack of a config isn't an error, we should just use the defaults.