Next 15 upgrade - awaiting function in server component causes component to restart infinitely
Unanswered
Braconid wasp posted this in #help-forum
Braconid waspOP
I tried upgrading my company's project to Next 15, but every page just loads forever when calling
When I tried debugging why, I found that when an async function
The function looks like this:
And the i18n functions are generated by this library: https://github.com/ivanhofer/typesafe-i18n
The rerurn value is something like:
I determined this by added console logs, and confirmed that the function does get called and concludes; but even after that function returns, the page component doesn't continue and appears to start over. If I remove the call to this function, then it proceeds properly.
Any ideas? Thanks! Would love to upgrade to Next 15.
next dev
regardless of the --turbopack
flag or not.When I tried debugging why, I found that when an async function
getI18nOnServer()
that we have gets called and awaited on from any server component, it seems to successfully invoke the function; but when it returns, the page component appears to start over again from the start instead of continuing onwards.The function looks like this:
import { headers } from "next/headers";
import {
localeHeaderKey,
pathnameHeaderKey,
searchHeaderKey,
} from "~/injected-headers";
import { baseLocale, i18n } from "~/typesafe-i18n/gen-util";
import { loadLocale } from "~/typesafe-i18n/gen-util.sync";
import { localeSchema } from "~/utils/i18n/locale";
import "server-only";
export async function getLocaleFromHeaderOnServer() {
// https://github.com/vercel/next.js/discussions/43179
// we get this from a header so that we can get the locale from anywhere
// serverside
const requestHeaders = await headers();
return localeSchema
.catch(baseLocale)
.default(baseLocale)
.parse(requestHeaders.get(localeHeaderKey));
}
/**
* @returns The translation functions for the locale that was detected from the
* header
*/
export async function getI18nOnServer() {
const locale = await getLocaleFromHeaderOnServer();
loadLocale(locale);
return i18n()[locale];
}
And the i18n functions are generated by this library: https://github.com/ivanhofer/typesafe-i18n
The rerurn value is something like:
{ some_i18n_key: () => string }
; it's a fairly big set of functions that gets returned.I determined this by added console logs, and confirmed that the function does get called and concludes; but even after that function returns, the page component doesn't continue and appears to start over. If I remove the call to this function, then it proceeds properly.
Any ideas? Thanks! Would love to upgrade to Next 15.
9 Replies
Braconid waspOP
this function has
import "server only"
so i'm pretty certain it's not being called by a client component and incurring some kind of serialization issueso yeah idk this is very strange to me
Braconid waspOP
OK, i was able to make it work by not making that specific function
async
by hard-coding the localeso it is purely an issue about it being an async function
i don't know what voodoo magic NextJS is doing behind the scenes but whatever it is, it broke that library 🤣
That said, the maintainer of it passed away and it's almost certainly a dead library so not so important to support it I guess.
that said, I presume that this issue may affect some other libraries that also return complex, proxy-filled deeply nested objects with lots of functions, maybe
¯\_(ツ)_/¯
I won't mark this as solved since the core issue still is a thing, but yeah... for anyone updating to Next 15 you've been warned, there is weird stuff going on with
async/await