From 066c922f66d7ba8bc041f434fd1afd2dca947b48 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 23 Jan 2021 22:13:59 +0900 Subject: [PATCH] cache locale file --- src/client/sw/sw.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/client/sw/sw.ts b/src/client/sw/sw.ts index 6c7ab17852..0a23e51141 100644 --- a/src/client/sw/sw.ts +++ b/src/client/sw/sw.ts @@ -113,7 +113,21 @@ self.addEventListener('message', ev => { //#region Function: (Re)Load i18n instance async function fetchLocale() { - i18n = new I18n(await (await fetch(`/assets/locales/${lang}.${version}.json`)).json()); + //#region localeファイルの読み込み + // Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う + const localeUrl = `/assets/locales/${lang}.${version}.json`; + let localeRes = await caches.match(localeUrl); + + if (!localeRes) { + localeRes = await fetch(localeUrl); + const clone = localeRes?.clone() + if (!clone?.clone().ok) return; + + caches.open(cacheName).then(cache => cache.put(localeUrl, clone)); + } + + i18n = new I18n(await localeRes.json()); + //#endregion //#region i18nをきちんと読み込んだ後にやりたい処理 for (const { type, body } of pushesPool) {