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

Use .env variables on defineI18nConfig #2548

Closed
50l3r opened this issue Nov 9, 2023 · 4 comments
Closed

Use .env variables on defineI18nConfig #2548

50l3r opened this issue Nov 9, 2023 · 4 comments

Comments

@50l3r
Copy link

50l3r commented Nov 9, 2023

Environment



Reproduction

import type { LocaleMessage } from '@intlify/core-base'
import type { VueMessageType } from '@nuxtjs/i18n/dist/runtime/composables'

export default defineI18nConfig(async () => ({
    legacy: false,
    locale: 'es',
    messages: await loadLocale()
}))

type Strings = Record<string, LocaleMessage<VueMessageType>>

const loadLocale = (): Promise<Strings> => {
    return new Promise((resolve, reject) => {
        fetch(`${process.env.NUXT_PUBLIC_API_URL}/cms/strings`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
        })
            .then(async (res) => {
                const data = await res.json()
                resolve(data)
            })
            .catch((error) => {
                reject(error)
            })
    })
}

Describe the bug

I need to use environment variables inside defineI18nConfig but i recieve undefined in some cases:

[Vue Router warn]: No match found for location with path "/undefined/cms/strings"

I receive the correct string at the start but after a while it stops working.

chrome_ILuUy04jXB.mp4

There seems to be a point where I do get access to process.env.NUXT_PUBLIC_API_URL but it stops working after a while.

I cannot access useRuntimeConfig either because I am not in the nuxt context.

Additional context

No response

Logs

No response

Copy link
Collaborator

kazupon commented Nov 11, 2023

Hmm, it seems that it is not accessible in the defineI18nConfig called in the plugin. 🤔

@danielroe
The defineI18nConfig is loaded by a plugin defined by nuxt i18n via defineNuxtPlugin. As far as I am aware, it is then accessible via public values (useRuntimeConfig) defined in environment variables by nuxt. Why can't I access it on the plugin side?

@BobbieGoede
Copy link
Collaborator

@kazupon @danielroe
After trying to debug this issue, I found that useRuntimeConfig does not work (seems to work client side?) if the dynamic imports for the i18n configs are imported from within a function in the generated i18n.options.mjs file (this function gets called from plugin).

This does work if the imports happen directly inside the plugin, see BobbieGoede#18 (still need to clean it up a bit, hence PR on my fork). Maybe it is preferable to move that logic out of the generated file anyway so that the types get checked.

I don't know why this does work and the other approach does not 😅.. Maybe there is an easy way to debug this that I'm unaware of 🤔.

@BobbieGoede
Copy link
Collaborator

I'm closing this as it should be possible to use useRuntimeConfig on the edge release channel since #2252. Let me know if you're running into any issues, will reopen if that is the case!

@mctweb
Copy link

mctweb commented Nov 27, 2023

I'm still having issues with this -
[nuxt] [request error] [unhandled] [500] [nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at https://nuxt.com/docs/guide/concepts/auto-imports#using-vue-and-nuxt-composables.

This is using '"@nuxtjs/i18n": "8.0.0-rc.5",'

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

4 participants