Skip to content
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

Cannot use default translation values using Next's app folder structure #173

Closed
peterbrczan opened this issue Jan 26, 2023 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@peterbrczan
Copy link

Description

Hello there!

My issue is, that when I try to provide some default values for formatting the rich texts, like:

<NextIntlClientProvider
  locale={params.locale}
  messages={messages}
  defaultTranslationValues={{
    // This function breaks the app
    highlight: (text) => <strong>{text}</strong>,
  }}
>
  {children}
</NextIntlClientProvider>

then I got that error:

Error: Functions cannot be passed directly to Client Components because they're not serializable.

Is this issue might have been resolved already using the next-intl@2.11.0-beta.4 version?

Reproduction URL (CodeSandbox)

https://codesandbox.io/p/sandbox/next-intl-bug-template-forked-6jfg9x?file=%2Fsrc%2Fapp%2F%5Blocale%5D%2Flayout.tsx&selection=%5B%7B%22endColumn%22%3A38%2C%22endLineNumber%22%3A30%2C%22startColumn%22%3A38%2C%22startLineNumber%22%3A30%7D%5D

Reproduction description

  1. Migrate to NextJS's app folder.
  2. Set up next-intl based on the documentation..
  3. Create some rich text translations.
  4. Try to pass some value to the defaultTranslationValues prop of the NextIntlClientProvider component.
  5. The app's gonna crash

Expected behaviour

The app should not crash because of this, instead it should render the rich text message. If that's not possible because the nature of the server components, then it should provide a fallback value or remove this props and it's type declarations when using the app folder structure.

@peterbrczan peterbrczan added the bug Something isn't working label Jan 26, 2023
@amannn
Copy link
Owner

amannn commented Jan 27, 2023

Hey @peterbrczan,

thanks for the report! That's an expected error, React can not serialize functions like defaultTranslationValues when you pass them to a Client Component.

The fix is to create a Client Component where the prop is defined. Have a look at this fixed version of your CodeSandbox.

I'm not sure yet how much information next-intl needs to provide in regards to this, as this is a general Next.js/React topic. Next.js already has a small section in regards to this. Furthermore with the Server Components integration I think there'll be less need for this pattern.

Let me know if this helps!

@amannn amannn closed this as completed Jan 27, 2023
@peterbrczan
Copy link
Author

peterbrczan commented Jan 27, 2023

Hi @amannn

Thanks for your quick and detailed response, and furthermore for putting effort to extend the CodeSandbox example!

You answered and helped me a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants