File size: 1,567 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * versionStore - Build version information
 *
 * - `build`: llama.cpp build number from `build.json`, embedded at llama.cpp
 *   build time (LLAMA_BUILD_NUMBER). Shown in the UI when `showBuildVersion`
 *   is enabled.
 * - `frontend`: frontend build version from SvelteKit's `_app/version.json`,
 *   generated by the @vite-pwa/sveltekit plugin. Changes on every build, so
 *   comparing it against localStorage reliably detects server upgrades.
 *
 * In dev mode both fall back to `'dev'`.
 */

import { browser } from '$app/environment';
import { base } from '$app/paths';

class VersionStore {
	build = $state<string>('');
	frontend = $state<string>('');

	/**
	 * Fetch the version files. Called by initStores(); order-independent,
	 * so it runs in the background.
	 */
	initialize(): void {
		if (!browser) return;

		if (import.meta.env.DEV) {
			this.build = 'dev';
			this.frontend = 'dev';

			return;
		}

		void this.load();
	}

	private async load(): Promise<void> {
		try {
			const res = await fetch(`${base}/build.json`, { cache: 'no-store' });

			if (res.ok) {
				const data = await res.json();

				this.build = data.version ?? '';
			}
		} catch {
			// build.json missing or unreachable - leave as empty string
		}

		try {
			const res = await fetch(`${base}/_app/version.json`, { cache: 'no-store' });

			if (res.ok) {
				const data = await res.json();

				this.frontend = data.version ?? '';
			}
		} catch {
			// version.json missing or unreachable - leave as empty string
		}
	}
}

export const versionStore = new VersionStore();