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

feat!: Inherit context in case nested NextIntlClientProvider instances are present #1413

Merged
merged 4 commits into from
Oct 9, 2024

Conversation

amannn
Copy link
Owner

@amannn amannn commented Oct 9, 2024

If you have nested providers, previously only the configuration of the innermost one would be applied.

With this change, configuration is now passed from one provider to the next, while allowing to override individual props. This simplifies the configuration of onError and getMessageFallback if you're using those (see proposed docs).

Breaking change: There's a very rare chance that this change affects your app, but in case you've previously relied on providers not inheriting from each other, you now have to reset props manually in case you want to retain the previous behavior.

Copy link

vercel bot commented Oct 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-intl-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 9, 2024 5:36pm
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 9, 2024 5:36pm
next-intl-example-app-router-without-i18n-routing ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 9, 2024 5:36pm

messages: messages || prevContext?.messages,
now: now || prevContext?.now,
onError: onError || prevContext?.onError,
timeZone: timeZone || prevContext?.timeZone
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't minify terribly well, but can't really think of a better solution …

@amannn amannn mentioned this pull request Oct 9, 2024
8 tasks
@amannn amannn changed the title feat: Inherit context feat: Inherit context between providers Oct 9, 2024
@amannn amannn merged commit d80d691 into v4 Oct 9, 2024
9 checks passed
@amannn amannn deleted the feat/inherit-context branch October 24, 2024 13:31
@amannn amannn changed the title feat!: Inherit context between providers feat!: Inherit context in case multiple NextIntlClientProvider ancestors are present Oct 31, 2024
@amannn amannn changed the title feat!: Inherit context in case multiple NextIntlClientProvider ancestors are present feat!: Inherit context in case nested NextIntlClientProvider instances are present Oct 31, 2024
@dmitrc
Copy link

dmitrc commented Jan 3, 2025

@amannn, thanks for your great work as always!

I was wondering if it also makes sense to optionally allow the user to merge the existing and current messages as well, eg to support the nested providers where you provide essential client messages in the root layout, and then component-specific messages later down the tree?

@amannn
Copy link
Owner Author

amannn commented Jan 5, 2025

@dmitrc Thank you for the kind words and happy new year!

Merging such messages is certainly an interesting use case, yes. Currently, you could achieve this manually in userland.

Ideally, with #1, relevant messages for Client Components would be picked automatically—not requiring the developer to think in terms of "essential" client messages and "component-specific" ones.

#1 and #962 are the two main features I'd like to explore next for next-intl. If this works out well, I think we might not have to add merging of messages to next-intl. Due to this, I think for the time being I'd not add this behavior to next-intl.

Does that sound right to you?

@dmitrc
Copy link

dmitrc commented Jan 20, 2025

Due to this, I think for the time being I'd not add this behavior to next-intl.
Does that sound right to you?

Yup, this makes complete sense, and I am incredibly excited about the improvements that solving #1 would bring!

The only challenge in implementing it in userland is that it requires some opinionated setup:

  • We can't obtain the whole IntlContext as it is not exposed, so we have to do it piecewise
  • We can obtain the messages via useMessages to merge them, but the hook will throw if it hasn't been set earlier
  • Since wrapping hooks in try/catch is a no-no, we have to ensure to set <NextIntlClientProvider messages={{}}> at the root
  • Subsequently our own <NestedNextIntlClientProvider ...> can be used to merge the new messages with prior ones

Not too big of a deal, but the second bullet caught me by surprise a little :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants