From a44ad6344069ba9f1bfefc018313d6b46958aeef Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Sat, 16 Jan 2021 13:46:46 +0900
Subject: [PATCH] Better boot handling

---
 src/client/init.ts     |  4 ++--
 src/server/web/boot.js | 12 ++++--------
 2 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/src/client/init.ts b/src/client/init.ts
index 554b4b3320..f329d22251 100644
--- a/src/client/init.ts
+++ b/src/client/init.ts
@@ -60,8 +60,6 @@ import { getThemes } from './theme-store';
 
 console.info(`Misskey v${version}`);
 
-window.clearTimeout((window as any).mkBootTimer);
-
 if (_DEV_) {
 	console.warn('Development mode!!!');
 
@@ -170,6 +168,8 @@ if ($i && $i.token) {
 //#endregion
 
 fetchInstance().then(() => {
+	localStorage.setItem('v', instance.version);
+
 	// Init service worker
 	//if (this.store.state.instance.meta.swPublickey) this.registerSw(this.store.state.instance.meta.swPublickey);
 });
diff --git a/src/server/web/boot.js b/src/server/web/boot.js
index aab986ba28..eb7c21fb63 100644
--- a/src/server/web/boot.js
+++ b/src/server/web/boot.js
@@ -50,14 +50,9 @@
 	script.setAttribute('src', `/assets/app.${v}.js${salt}`);
 	script.setAttribute('async', 'true');
 	script.setAttribute('defer', 'true');
-	head.appendChild(script);
+	script.addEventListener('error', async () => {
+		document.documentElement.innerHTML = '読み込みに失敗しました。';
 
-	// 3秒経ってもスクリプトがロードされない場合はバージョンが古くて
-	// 404になっているせいかもしれないので、バージョンを確認して古ければ更新する
-	//
-	// 読み込まれたスクリプトからこのタイマーを解除できるように、
-	// グローバルにタイマーIDを代入しておく
-	window.mkBootTimer = window.setTimeout(async () => {
 		// TODO: サーバーが落ちている場合などのエラーハンドリング
 		const res = await fetch('/api/meta', {
 			method: 'POST',
@@ -74,7 +69,8 @@
 				'New version of Misskey available. The page will be reloaded.');
 			refresh();
 		}
-	}, 3000);
+	});
+	head.appendChild(script);
 	//#endregion
 
 	//#region Theme