Skip to content

Commit

Permalink
fix: memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
s00d committed Aug 22, 2024
1 parent 845cd0e commit c465d22
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/runtime/plugins/01.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 4 additions & 8 deletions src/runtime/translationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,17 @@ export function useTranslationHelper() {
},
loadPageTranslations: async (locale: string, routeName: string, translations: Translations): Promise<void> => {
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)
}
},
loadTranslations: async (locale: string, translations: Translations): Promise<void> => {
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)
Expand Down

0 comments on commit c465d22

Please sign in to comment.