fix locales versioning in service worker

This commit is contained in:
Hazelnoot 2024-10-15 21:39:20 -04:00
parent b5a1c54d65
commit 45ac7e50bc
4 changed files with 6 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild'; import * as esbuild from 'esbuild';
import locales from '../../locales/index.js'; import locales from '../../locales/index.js';
import meta from '../../package.json' with { type: 'json' }; import meta from '../../package.json' with { type: 'json' };
import { localesVersion } from '../../locales/version.js';
const watch = process.argv[2]?.includes('watch'); const watch = process.argv[2]?.includes('watch');
const __dirname = fileURLToPath(new URL('.', import.meta.url)); const __dirname = fileURLToPath(new URL('.', import.meta.url));
@ -23,6 +24,7 @@ const buildOptions = {
_DEV_: JSON.stringify(process.env.NODE_ENV !== 'production'), _DEV_: JSON.stringify(process.env.NODE_ENV !== 'production'),
_ENV_: JSON.stringify(process.env.NODE_ENV ?? ''), // `NODE_ENV`が`undefined`なとき`JSON.stringify`が`undefined`を返してエラーになってしまうので`??`を使っている _ENV_: JSON.stringify(process.env.NODE_ENV ?? ''), // `NODE_ENV`が`undefined`なとき`JSON.stringify`が`undefined`を返してエラーになってしまうので`??`を使っている
_LANGS_: JSON.stringify(Object.entries(locales).map(([k, v]) => [k, v._lang_])), _LANGS_: JSON.stringify(Object.entries(locales).map(([k, v]) => [k, v._lang_])),
_LANGS_VERSION_: JSON.stringify(localesVersion),
_PERF_PREFIX_: JSON.stringify('Misskey:'), _PERF_PREFIX_: JSON.stringify('Misskey:'),
_VERSION_: JSON.stringify(meta.version), _VERSION_: JSON.stringify(meta.version),
}, },

View file

@ -12,6 +12,7 @@ export default [
require: false, require: false,
_DEV_: false, _DEV_: false,
_LANGS_: false, _LANGS_: false,
_LANGS_VERSION_: false,
_VERSION_: false, _VERSION_: false,
_ENV_: false, _ENV_: false,
_PERF_PREFIX_: false, _PERF_PREFIX_: false,

View file

@ -7,6 +7,7 @@
type FIXME = any; type FIXME = any;
declare const _LANGS_: string[][]; declare const _LANGS_: string[][];
declare const _LANGS_VERSION_: string;
declare const _VERSION_: string; declare const _VERSION_: string;
declare const _ENV_: string; declare const _ENV_: string;
declare const _DEV_: boolean; declare const _DEV_: boolean;

View file

@ -11,7 +11,7 @@ import { I18n } from '@@/js/i18n.js';
import type { Locale } from '../../../../locales/index.js'; import type { Locale } from '../../../../locales/index.js';
class SwLang { class SwLang {
public cacheName = `mk-cache-${_VERSION_}`; public cacheName = `mk-cache-${_LANGS_VERSION_}`;
public lang: Promise<string> = get('lang').then(async prelang => { public lang: Promise<string> = get('lang').then(async prelang => {
if (!prelang) return 'en-US'; if (!prelang) return 'en-US';
@ -32,7 +32,7 @@ class SwLang {
private async _fetch(): Promise<I18n<Locale>> { private async _fetch(): Promise<I18n<Locale>> {
// Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う // Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う
const localeUrl = `/assets/locales/${await this.lang}.${_VERSION_}.json`; const localeUrl = `/assets/locales/${await this.lang}.${_LANGS_VERSION_}.json`;
let localeRes = await caches.match(localeUrl); let localeRes = await caches.match(localeUrl);
// _DEV_がtrueの場合は常に最新化 // _DEV_がtrueの場合は常に最新化