From c465d2280ff35d28daabc94fd8a32e1be28ce224 Mon Sep 17 00:00:00 2001 From: Pavel Kuzmin Date: Thu, 22 Aug 2024 15:03:52 +0500 Subject: [PATCH] fix: memory leak --- src/runtime/plugins/01.plugin.ts | 13 ++++++++----- src/runtime/translationHelper.ts | 12 ++++-------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/runtime/plugins/01.plugin.ts b/src/runtime/plugins/01.plugin.ts index 5a8333d5..3da79c60 100644 --- a/src/runtime/plugins/01.plugin.ts +++ b/src/runtime/plugins/01.plugin.ts @@ -141,17 +141,20 @@ export default defineNuxtPlugin(async (nuxtApp) => { const initialLocale = (route.params?.locale ?? i18nConfig.defaultLocale).toString() const initialRouteName = (route.name as string).replace(`localized-`, '') - const data: Translations = await $fetch(`/_locales/general/${initialLocale}/data.json?v=${i18nConfig.dateBuild}`, { baseURL: i18nConfig.baseURL }) - await i18nHelper!.loadTranslations(initialLocale, data ?? {}) - + if (!i18nHelper!.hasGeneralTranslation(initialLocale)) { + const data: Translations = await $fetch(`/_locales/general/${initialLocale}/data.json?v=${i18nConfig.dateBuild}`, { baseURL: i18nConfig.baseURL }) + await i18nHelper!.loadTranslations(initialLocale, data ?? {}) + } if (import.meta.server) { const locale = (route.params?.locale ?? i18nConfig.defaultLocale).toString() let routeName = initialRouteName if (i18nConfig.routesLocaleLinks && i18nConfig.routesLocaleLinks[routeName]) { routeName = i18nConfig.routesLocaleLinks[routeName] } - const data: Translations = await $fetch(`/_locales/${routeName}/${locale}/data.json?v=${i18nConfig.dateBuild}`, { baseURL: i18nConfig.baseURL }) - await i18nHelper!.loadPageTranslations(initialLocale, initialRouteName, data ?? {}) + if (!i18nHelper!.hasPageTranslation(initialLocale, routeName)) { + const pageData: Translations = await $fetch(`/_locales/${routeName}/${locale}/data.json?v=${i18nConfig.dateBuild}`, { baseURL: i18nConfig.baseURL }) + await i18nHelper!.loadPageTranslations(initialLocale, initialRouteName, pageData ?? {}) + } } return { diff --git a/src/runtime/translationHelper.ts b/src/runtime/translationHelper.ts index 0e027541..44d44a43 100644 --- a/src/runtime/translationHelper.ts +++ b/src/runtime/translationHelper.ts @@ -125,10 +125,8 @@ export function useTranslationHelper() { }, loadPageTranslations: async (locale: string, routeName: string, translations: Translations): Promise => { try { - if (!routeLocaleCache[`${locale}:${routeName}`]) { - routeLocaleCache[`${locale}:${routeName}`] = { ...translations } - serverTranslationInit[`${locale}:${routeName}`] = true - } + routeLocaleCache[`${locale}:${routeName}`] = { ...translations } + serverTranslationInit[`${locale}:${routeName}`] = true } catch (error) { console.error(`Error loading translations for ${locale} and ${routeName}:`, error) @@ -136,10 +134,8 @@ export function useTranslationHelper() { }, loadTranslations: async (locale: string, translations: Translations): Promise => { try { - if (!generalLocaleCache[locale]) { - generalLocaleCache[locale] = { ...translations } - serverTranslationInit[`${locale}:index`] = true - } + generalLocaleCache[locale] = { ...translations } + serverTranslationInit[`${locale}:index`] = true } catch (error) { console.error(`Error loading translations for general ${locale}:`, error)