useTranslations in RSC breaks static export (...because it used headers()) #1674
Replies: 3 comments 2 replies
-
I tried this but it gives the error that useTranslations can't be used in an async component export default async function Home({
params
}: {
params: Promise<{ locale: Locale }>
}) {
const { locale } = await params
setRequestLocale(locale)
const t = useTranslations("index")
} |
Beta Was this translation helpful? Give feedback.
-
Funnily enough, this fixes it but I don't understand why export default async function Home({
params
}: {
params: Promise<unknown>
}) {
await params
const t = await getTranslations("index")
} If I don't await params here, it will break during static export. It's weird because I don't have to setRequestLocale? Well I'm doing that in my layout... Maybe that's why? It needs to wait for that? I might have made a discovery about the setRequestLocale thing only needing to be called in the [locale] layout. It appears to propagate to every other server component page that awaits params |
Beta Was this translation helpful? Give feedback.
-
Hey, the docs cover all the aspects you're asking about: Please make sure to check there first! 🙂
Nope, while it might work sometimes, there's a race condition. For it to work reliably, you need to add As a side note, static rendering should hopefully get easier in the near future: #663 (comment). I know it's unfortunately a bit cumbersome currently. I'll move this issue to a discussion since it's a usage question. |
Beta Was this translation helpful? Give feedback.
-
Description
Howdy. Love the library, using it with Next.js App Router. Got it working pretty well for a hybrid application setup to run both as RSC's and as static exports. Only one issue I'm running into...
I'm able to useTranslations just fine in server components, this is great for SSR. It works in npm run dev, it works in production on Vercel, very good.
However, once I do my static export, I get the following error:
Here is my page
Sadly I have to add "use client" everywhere I am using useTranslations, which is only required for static exports. I wish there was some way to bypass this, so I didn't have to duplicate tons of components into client versions, just to use translations on them, specifically for static export. Since the RSC's work just fine and do compile into static pages without useTranslations
Verifications
Mandatory reproduction URL
https://newtech.dev
Reproduction description
useTranslations in a server component with output: "export"
Expected behaviour
No error, translations just work regardless of static export when using useTranslations hook, skips the headers() check during static exports, or skips it altogether?
Beta Was this translation helpful? Give feedback.
All reactions