Closed fakedob closed 8 months ago
Would you be able to provide a reproduction? π
Running into this on a Vercel deployment. First request fails, following requests go through. It's a private repo so sorry for not being able to offer a reproduction yet, I'll see if I can come up with one.
Closing this due to inactivity and lack of reproduction, if you're still experiencing this issue please open a new issue with a (minimal) reproduction.
I understand that it's not possible to share private code, or that it's difficult to replicate due to the environment, but for us to be able to debug this issue we would have to put in the time and effort to figure out how to replicate your issue by ourselves.
Hey, I'm having the same issue here. My app runs on a serverless environment, and this issue happens a lot on "cold start". It works fine on the coming requests when the instance warms up. I'm using @nuxtjs/i18n@8.0.0.
Same here
Unfortunately same issue here, also in dev mode
Unfortunately same issue here, also in dev mode
Hey @bart, if it happens in Dev, can you share a small reproduction? Here we all experience this issue only on prod, which makes it impossible to give feedback on the real cause. If this can be tested in dev, then we can see where the error comes from βοΈ
Sure @fakedob I'm also diving into it already. useTranslation()
is checking for event.context.i18n and if this is null it is throwing the error. Here is a reproduction link: https://stackblitz.com/~/github.com/bart/i18n-repro
@fakedob Did you have the chance to look into it already?
All right!! After digging deep into the module I figured out what the reason for this issue was. Module checks for enableServerIntegration
to load server-side middleware that registers i18n there. Problem: When configuring i18n inside of the modules
key it doesn't work because experimental gets loaded as undefined
. When using the i18n key instead for configuration, it works. Maybe that's also interesting for others who are facing the same issue.
// nuxt.confg.js - not working
export default defineNuxtConfig({
compatibilityDate: '2024-04-03',
devtools: { enabled: true },
modules: [["@nuxtjs/i18n", {
experimental: {
localeDetector: "./localeDetector.ts",
},
defaultLocale: "en",
locales: [
{
code: "en",
name: "English",
file: "en.yaml",
},
],
langDir: "locales",
}]]
})
// nuxt.config.js - working
export default defineNuxtConfig({
compatibilityDate: '2024-04-03',
devtools: { enabled: true },
modules: ["@nuxtjs/i18n"],
i18n: {
experimental: {
localeDetector: "./localeDetector.ts",
},
defaultLocale: "en",
locales: [
{
code: "en",
name: "English",
file: "en.yaml",
},
],
langDir: "locales",
}
})
@bart Thanks for investigating into the source of the issue! This should get resolved by #3020.
All right!! After digging deep into the module I figured out what the reason for this issue was. Module checks for
enableServerIntegration
to load server-side middleware that registers i18n there. Problem: When configuring i18n inside of themodules
key it doesn't work because experimental gets loaded asundefined
. When using the i18n key instead for configuration, it works. Maybe that's also interesting for others who are facing the same issue.// nuxt.confg.js - not working export default defineNuxtConfig({ compatibilityDate: '2024-04-03', devtools: { enabled: true }, modules: [["@nuxtjs/i18n", { experimental: { localeDetector: "./localeDetector.ts", }, defaultLocale: "en", locales: [ { code: "en", name: "English", file: "en.yaml", }, ], langDir: "locales", }]] }) // nuxt.config.js - working export default defineNuxtConfig({ compatibilityDate: '2024-04-03', devtools: { enabled: true }, modules: ["@nuxtjs/i18n"], i18n: { experimental: { localeDetector: "./localeDetector.ts", }, defaultLocale: "en", locales: [ { code: "en", name: "English", file: "en.yaml", }, ], langDir: "locales", } })
Hey @bart , thank you for your contribution and sorry for my late reply, I was on holiday. Unfortunately, for me it doesnt solve the issue and the key in my nuxt.config is correct. As far as I can understand, the i18n config gets set in an async way, that doesnt wait for the configuration to be setup, prior executing the first response. Based on my experiments, in few ms after the first call, its already set and every next works normally. On my end, this cannot be replicated in normal dev or host environment, it must be serverless host, that has a cold start, which means, the app must be build specifically for this type of hosting and executed in a cold start, to be able to reproduce it. Without knowing more of the internals of how this module works, I cannot say more at this point, I did not had any chance to look deeper into the code, but I am sure it is arround the setup of the i18n initial config being done in async without being awaited.
I just tried to setup server side translations following the documentation on version 8.4.0 - the same error appears:
middleware not initialized, please setup onRequest and onAfterResponse options of createApp with the middleware obtained with defineI18nMiddleware
and I have the config as @bart suggests.
Why is this issue closed?
@nstdspace Can you open a new issue with a minimal reproduction? π
@nstdspace
Can you open a new issue with a minimal reproduction? π
Hey @BobbieGoede as in my initial post, I tried to explain the problem does not come from client code. It can only be reproduced in environment that can do cold start as the serveless framework does. If you want, i think i can make you a serverless npm project for you to run and test, but for the i18n module, its basically the initial config as in documentation is enough to reproduce.
My best guess is that the config in the nuxt.config gets loaded async and thats why the initial request fails, as the condig has not been set up.
You cannot reproduce this in dev environment, since you run a dev server and when you hit the api, the config has already been loaded.
In serverless, your server is dead until someone request something from it and the process of the server is being handled differently. Thats why we experience the problem only when deployed on such infrastructure.
@fakedob If you can create a minimal reproduction with instructions on how to run it in a serverless environment (because I haven't done this before), or are able to reproduce the issue by replicating the cold start somehow, that would allow me to investigate the issue further.
From what I can tell the Nuxt config is not loaded asynchronously so I wonder what else it could be π€
Environment
Darwin
v18.15.0
3.9.3
3.10.0
2.8.1
yarn@1.22.19
-
pwa
,telemetry
,srcDir
,dir
,components
,sourcemap
,generate
,runtimeConfig
,build
,transition
,ignore
,modules
,serverHandlers
,vue
,vite
,hooks
,devtools
,nitro
,i18n
,vueEmail
,postcss
nuxt-multi-cache@3.1.1
,@pinia/nuxt@0.5.1
,@nuxtjs/i18n@8.0.0
,@nuxtjs/tailwindcss@6.10.4
,@vue-email/nuxt@0.8.8
,@tresjs/nuxt@2.0.2
,@vite-pwa/nuxt@0.4.0
,nuxt-simple-sitemap@4.4.1
,@nuxtjs/robots@3.0.0
-
Reproduction
Wont be able to provide reproduction, as in reality, it will require access to the host in serverless environment
Describe the bug
I've setup SSR translations with
In my event handler, I call
useTranslation
to access thet
function, as follows:All this is working as expected when run in development. In our cloud, we use serverless framework, which scales our application. There is a "cold start" procedure, which runs when the app was either not used for some time, or needs to start a new instance. That is the reason why I am able to hit the issue, as on other build types (or dev), the module loads, prior making the first request directly into the handler, that uses the
t
function.If, when the app was called directly on the event handler that uses the
useTranslations
, the nuxtConfig for i18n is not yet available in the context, thus, resulting in "middleware not initialized, please setuponRequest
andonAfterResponse
options ofcreateApp
with the middleware obtained withdefineI18nMiddleware
". It also worth mentioning, that there is a period of 1-3 seconds between the initial app load and when the config is available in the context. My best bet is, that the nuxt/i18n module is preparing the config in async, before appending it to the context of the request.So far I am out of clue, why is this happening. Seems like the event handler gets called, prior the i18n was properly loaded into context.
Additional context
No response
Logs
No response