diff --git a/src/state/persisted/index.ts b/src/state/persisted/index.ts index 6f4beae2ca..834871ba09 100644 --- a/src/state/persisted/index.ts +++ b/src/state/persisted/index.ts @@ -19,7 +19,7 @@ let _state: Schema = defaults export async function init() { const stored = await readFromStorage() if (stored) { - _state = stored + _state = normalizeData(stored) } } init satisfies PersistedApi['init'] @@ -33,10 +33,10 @@ export async function write( key: K, value: Schema[K], ): Promise { - _state = { + _state = normalizeData({ ..._state, [key]: value, - } + }) await writeToStorage(_state) } write satisfies PersistedApi['write'] @@ -84,3 +84,41 @@ async function readFromStorage(): Promise { return tryParse(rawData) } } + +function normalizeData(data: Schema) { + /** + * Normalize language prefs to ensure that these values only contain 2-letter + * country codes without region. + */ + try { + const next = {...data.languagePrefs} + next.primaryLanguage = next.primaryLanguage.split('-')[0] + next.contentLanguages = next.contentLanguages.map(lang => + normalizeLocaleToTwoLetterCode(lang), + ) + next.postLanguage = next.postLanguage + .split(',') + .map(lang => normalizeLocaleToTwoLetterCode(lang)) + .filter(Boolean) + .join(',') + next.postLanguageHistory = next.postLanguageHistory.map(postLanguage => { + return postLanguage + .split(',') + .map(lang => normalizeLocaleToTwoLetterCode(lang)) + .filter(Boolean) + .join(',') + }) + // mutate last in case anything above fails + data.languagePrefs = next + } catch (e: any) { + logger.error(`persisted state: failed to normalize language prefs`, { + safeMessage: e.message, + }) + } + + return data +} + +function normalizeLocaleToTwoLetterCode(lang: string) { + return lang.split('-')[0] +}