Skip to content

Commit

Permalink
Pre-fetch the config.json to improve startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Nov 15, 2024
1 parent 9650ca9 commit 2e172aa
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 6 additions & 10 deletions src/config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 2e172aa

Please sign in to comment.