Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt i18n caching not working when loading locales via API #3346

Open
sparrow-chik-chrk opened this issue Feb 3, 2025 · 0 comments
Open

Comments

@sparrow-chik-chrk
Copy link

sparrow-chik-chrk commented Feb 3, 2025

Environment



Reproduction

  1. Configure nuxt.config.ts with lazy: true and API-based locale loading:
    export default defineNuxtConfig({
      i18n: {
     locales: [
       {
         code: 'en',
         language: 'en-US',
         files: [
           { path: 'en-US.ts', cache: true },
           { path: 'strapi.ts', cache: true },
         ],
       },
       {
         code: 'uk',
         language: 'uk-UA',
         files: [
           { path: 'uk-UA.ts', cache: true },
           { path: 'strapi.ts', cache: true },
         ],
       },
     ],
     lazy: true,
     ...
     },
    })
  2. Use defineI18nLocale to fetch locale messages from an API:
       export default defineI18nLocale(async locale => {
         try {
           const res = await $fetch<{
             data?: SiteLocaleTextCollection[]
           }>(`/api/site-locale-texts?filters[locale_code][$eq]=${locale}`, {
             headers: {
               'Content-Type': 'application/json'
             }
           });
           return res.data?.length ? res.data.at(0)?.json : {};
         } catch (error) {
           console.warn(error);
           return {};
         }
       });
  3. Switch between locales multiple times in the UI.

Describe the bug

When switching locales, Nuxt i18n sends a request to the server every time the locale changes, instead of using cached messages. According to the documentation, caching should be enabled by default for static files and configurable for API-loaded locales, but in this case, it seems to be ignored.

Expected behavior:
After the first request, the locale messages should be cached and reused instead of refetching them on every locale change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant