Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

useTranslations in RSC breaks static export (...because it used headers()) #1672

Closed
3 tasks done
daveycodez opened this issue Jan 22, 2025 · 3 comments
Closed
3 tasks done
Labels
bug Something isn't working unconfirmed Needs triage.

Comments

@daveycodez
Copy link

daveycodez commented Jan 22, 2025

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:

Error: Route /[locale] with `dynamic = "error"` couldn't be rendered statically because it used `headers`.

Here is my page

import { useTranslations } from "next-intl"

export default function Home() {
    const t = useTranslations("index")

    return (
        <main className="my-auto max-w-3xl items-center mx-auto gap-10 flex flex-col text-center p-4">
            <div className="flex flex-col gap-6">
                <h1>
                    Next.js Capacitor
                    {" "}
                    {t("hybrid")}
                </h1>

                <p className="text-balance text-muted-foreground lg:text-lg">
                    <b className="text-foreground">
                        NEW-TECH
                    </b>

                    {" "}
                    {t("description_1")}
                </p>

                <p className="text-balance text-muted-foreground lg:text-lg">
                    <b className="text-foreground">
                        NEW-TECH
                    </b>

                    {" "}
                    {t("description_2")}
                </p>
            </div>
        </main>
    )
}

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?

@daveycodez daveycodez added bug Something isn't working unconfirmed Needs triage. labels Jan 22, 2025
@daveycodez
Copy link
Author

daveycodez commented Jan 22, 2025

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")
}

@daveycodez
Copy link
Author

daveycodez commented Jan 22, 2025

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

@amannn
Copy link
Owner

amannn commented Jan 22, 2025

Hey, the docs cover all the aspects you're asking about:

  1. Static rendering
  2. Static export
  3. Async components

Please make sure to check there first! 🙂

I might have made a discovery about the setRequestLocale thing only needing to be called in the [locale] layout.

Nope, while it might work sometimes, there's a race condition. For it to work reliably, you need to add setRequestLocale to all relevant layouts and pages.

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.

Repository owner locked and limited conversation to collaborators Jan 22, 2025
@amannn amannn converted this issue into discussion #1674 Jan 22, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug Something isn't working unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

2 participants