summaryrefslogtreecommitdiff
path: root/src/client/app/boot.js
blob: 54397c98c69f89bfc1da5eb519931badab256e8a (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * MISSKEY BOOT LOADER
 * (ENTRY POINT)
 */

/**
 * ドメインに基づいて適切なスクリプトを読み込みます。
 * ユーザーの言語およびモバイル端末か否かも考慮します。
 * webpackは介さないためrequireやimportは使えません。
 */

'use strict';

(function() {
	// キャッシュ削除要求があれば従う
	if (localStorage.getItem('shouldFlush') == 'true') {
		refresh();
		return;
	}

	//#region Load settings
	let settings = null;
	const vuex = localStorage.getItem('vuex');
	if (vuex) {
		settings = JSON.parse(vuex);
	}
	//#endregion

	// Get the current url information
	const url = new URL(location.href);

	//#region Detect app name
	let app = null;

	if (`${url.pathname}/`.startsWith('/docs/')) app = 'docs';
	if (`${url.pathname}/`.startsWith('/dev/')) app = 'dev';
	if (`${url.pathname}/`.startsWith('/auth/')) app = 'auth';
	//#endregion

	//#region Detect the user language
	let lang = null;

	if (LANGS.includes(navigator.language)) {
		lang = navigator.language;
	} else {
		lang = LANGS.find(x => x.split('-')[0] == navigator.language);

		if (lang == null) {
			// Fallback
			lang = 'en-US';
		}
	}

	if (settings && settings.device.lang &&
		LANGS.includes(settings.device.lang)) {
		lang = settings.device.lang;
	}
	//#endregion

	// Detect the user agent
	const ua = navigator.userAgent.toLowerCase();
	const isMobile = /mobile|iphone|ipad|android/.test(ua);

	// Get the <head> element
	const head = document.getElementsByTagName('head')[0];

	// If mobile, insert the viewport meta tag
	if (isMobile) {
		const meta = document.createElement('meta');
		meta.setAttribute('name', 'viewport');
		meta.setAttribute('content',
			'width=device-width,' +
			'initial-scale=1,' +
			'minimum-scale=1,' +
			'maximum-scale=1,' +
			'user-scalable=no');
		head.appendChild(meta);
	}

	// Switch desktop or mobile version
	if (app == null) {
		app = isMobile ? 'mobile' : 'desktop';
	}

	// Dark/Light
	if (settings) {
		if (settings.device.darkmode) {
			document.documentElement.setAttribute('data-darkmode', 'true');
		}
	}

	// Script version
	const ver = localStorage.getItem('v') || VERSION;

	// Get salt query
	const salt = localStorage.getItem('salt')
		? '?salt=' + localStorage.getItem('salt')
		: '';

	// Load an app script
	// Note: 'async' make it possible to load the script asyncly.
	//       'defer' make it possible to run the script when the dom loaded.
	const script = document.createElement('script');
	script.setAttribute('src', `/assets/${app}.${ver}.${lang}.js${salt}`);
	script.setAttribute('async', 'true');
	script.setAttribute('defer', 'true');
	head.appendChild(script);

	// 3秒経ってもスクリプトがロードされない場合はバージョンが古くて
	// 404になっているせいかもしれないので、バージョンを確認して古ければ更新する
	//
	// 読み込まれたスクリプトからこのタイマーを解除できるように、
	// グローバルにタイマーIDを代入しておく
	window.mkBootTimer = window.setTimeout(async () => {
		// Fetch meta
		const res = await fetch('/api/meta', {
			method: 'POST',
			cache: 'no-cache'
		});

		// Parse
		const meta = await res.json();

		// Compare versions
		if (meta.clientVersion != ver) {
			localStorage.setItem('v', meta.clientVersion);

			alert(
				'Misskeyの新しいバージョンがあります。ページを再度読み込みします。' +
				'\n\n' +
				'New version of Misskey available. The page will be reloaded.');

			refresh();
		}
	}, 3000);

	function refresh() {
		localStorage.setItem('shouldFlush', 'false');

		// Random
		localStorage.setItem('salt', Math.random().toString());

		// Clear cache (serive worker)
		try {
			navigator.serviceWorker.controller.postMessage('clear');

			navigator.serviceWorker.getRegistrations().then(registrations => {
				registrations.forEach(registration => registration.unregister());
			});
		} catch (e) {
			console.error(e);
		}

		// Force reload
		location.reload(true);
	}
})();