Skip to content

Commit

Permalink
perf: ensure setupLocale doesn't fetch _locales/en/messages.json
Browse files Browse the repository at this point in the history
…twice
  • Loading branch information
davidmurdoch authored and HowardBraham committed Oct 11, 2024
1 parent e4c71b7 commit 5a18b3a
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions shared/lib/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ import getFirstPreferredLangCode from '../../app/scripts/lib/get-first-preferred
import { fetchLocale, loadRelativeTimeFormatLocaleData } from '../modules/i18n';
import switchDirection from './switch-direction';

const defaultLocale = 'en';
const _setupLocale = async (currentLocale) => {
const currentLocaleMessages = currentLocale
? await fetchLocale(currentLocale)
: {};
const enLocaleMessages = await fetchLocale('en');
const enRelativeTime = loadRelativeTimeFormatLocaleData(defaultLocale);
const enLocale = fetchLocale(defaultLocale);

await loadRelativeTimeFormatLocaleData('en');
if (currentLocale) {
await loadRelativeTimeFormatLocaleData(currentLocale);
const promises = [enRelativeTime, enLocale];
if (currentLocale === defaultLocale) {
// enLocaleMessages and currentLocaleMessages are the same; reuse enLocale
promises.push(enLocale); // currentLocaleMessages
} else if (currentLocale) {
// currentLocale does not match enLocaleMessages
promises.push(fetchLocale(currentLocale)); // currentLocaleMessages
promises.push(loadRelativeTimeFormatLocaleData(currentLocale));
} else {
// currentLocale is not set
promises.push(Promise.resolve({})); // currentLocaleMessages
}

const [, enLocaleMessages, currentLocaleMessages] = await Promise.all(
promises,
);
return { currentLocaleMessages, enLocaleMessages };
};

Expand Down

0 comments on commit 5a18b3a

Please sign in to comment.