-
-
Notifications
You must be signed in to change notification settings - Fork 207
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
getT on API route on Vercel (serverless) #484
Comments
Can confirm that this bug exists, it virtually makes next-translate impossible to use on an API. Here's a stacktrace I'm getting in Vercel (just trying to be helpful):
|
Thanks to @aralroca and @Fivedark :) I was having the exact same issue and after applying the solution above from @Fivedark, everything is working as expected now. For the locale, I'm passing the value to my api function. Here's my code in case it could help anyone else:
|
any idea how make it work guys ? |
It is also happening on localhost, not only Vercel. I mean that, when I start the server, if I load a page in the browser, the subsequent api calls work fine, and |
A simple workaround would be to load it manually: import i18n from '../i18n'
global.i18nConfig = i18n Before solving it, I would like to rethink the way of consuming the configuration... |
@aralroca with the workaround I only get the keys and not the values returned. And can the workaround be added once in the next.js project or do we have to add this on every api route? |
@goellner No, the workaround will need to be added in every api route where you use The following patch works for me until this issue is resolved: // monkey-patches.ts
import getT from "next-translate/getT";
import i18n from "../i18n";
export const ensureAvailabilityOfGetT = () => {
global.i18nConfig = i18n;
return getT;
}; and then: // some-api-route.ts
import { ensureAvailabilityOfGetT } from "../../../utils/api/utils/monkey-patches";
const parsedLocale = "...";
const getT = ensureAvailabilityOfGetT();
const errorsT = await getT(parsedLocale, "errors");
const feedbackT = await getT(parsedLocale, "feedback");
// do your thing Once |
@goellner I had the same problem than you, only keys where return and not the values, it was because my At first I had only
|
It's a shame that this bug/feature isn't implemented in a timely manner. Can you think about it again? |
@Marc-Hoch the project is open-source, feel free to implement and do a PR |
This is what I ended up doing:
|
First: Thanks for this great lib!
I'm using
next-translate
@ 1.0.1 andnext
@ 10.0.5, and I have trouble to usegetT
on my API route.The following error occurred in production on Vercel, when calling the API:
ERROR TypeError: Cannot read property 'loadLocaleFrom' of undefined
The API is called serverless on Vercel which mean the NodeJS
global
didn't contain thei18nConfig
, which leads to this error message.I tried to set the config to global inside the API route:
The error disappears, but I get only the translation key instead of the translated text.
You can reproduce this on your local machine:
Build and start Next and call the API directly without any page load, because the page load will add the config to global.
Any ideas how to resolve this?
The text was updated successfully, but these errors were encountered: