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!: Remove default of now={new Date()} from NextIntlClientProvider for usage with format.relativeTime (preparation for dynamicIO) #1536

Merged
merged 14 commits into from
Nov 14, 2024

Conversation

amannn
Copy link
Owner

@amannn amannn commented Nov 12, 2024

tldr; — Are you using format.relativeTime in Client Components? If not, you can skip this.


If you've used format.relativeTime in Client Components previously, you might have relied on a default of now={new Date()} being set on NextIntlClientProvider:

'use client';

function Component({date}) {
  const format = useFormatter();

  // Uses a global `now` by default
  format.relativeTime(date);
}

In preparation for dynamicIO and ppr, the default for now on NextIntlClientProvider has now been removed. Note however that if you're configuring a now value in i18n/request.ts, this is still inherited by NextIntlClientProvider.

Therefore, if you just want to restore the previous behavior, you can do so in i18n/request.ts:

import {getRequestConfig} from 'next-intl/server';
 
export default getRequestConfig(async () => {
  return {
    // Use this consistently in Server- and Client Components
    now: new Date()
 
    // ...
  };
});

However, if you want to prepare for upcoming rendering features in Next.js already, the relative time formatting docs now suggest to always provide now explicitly based on useNow():

import {useNow, useFormatter} from 'next-intl';
 
function FormattedDate({date}) {
  const now = useNow();
  const format = useFormatter();
 
  // Provide `now` explicitly based on `useNow()`
  format.relativeTime(date, now);
}

If you don't provide now globally, this has a few benefits:

  1. Enables granular caching with PPR
  2. Allows client-side components to render an updated value
  3. You can still provide now globally if relevant, e.g. for consistent results when running tests

Learn more in the docs for useNow.


Resolves #1464

Copy link

vercel bot commented Nov 12, 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 Nov 14, 2024 1:47pm
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 1:47pm
next-intl-example-app-router-without-i18n-routing ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 1:47pm

@amannn amannn changed the title feat: Groundwork for dynamicIO feat!: Only pass now to client side via NextIntlClientProvider if it's provided in i18n/request.ts Nov 13, 2024
@amannn amannn changed the title feat!: Only pass now to client side via NextIntlClientProvider if it's provided in i18n/request.ts feat!: Prepare for dynamicIO and ppr Nov 13, 2024
@amannn amannn changed the title feat!: Prepare for dynamicIO and ppr feat!: Only pass now to client side via NextIntlClientProvider in case it's defined in i18n/request.ts (preparation for dynamicIO) Nov 13, 2024
@amannn amannn mentioned this pull request Nov 13, 2024
6 tasks
@amannn amannn changed the title feat!: Only pass now to client side via NextIntlClientProvider in case it's defined in i18n/request.ts (preparation for dynamicIO) feat!: Remove default of now={new Date()} from NextIntlClientProvider for usage in format.relativeTime(…) Nov 14, 2024
@amannn amannn changed the title feat!: Remove default of now={new Date()} from NextIntlClientProvider for usage in format.relativeTime(…) feat!: Remove default of now={new Date()} from NextIntlClientProvider for usage with format.relativeTime Nov 14, 2024
@amannn amannn marked this pull request as ready for review November 14, 2024 13:31
@amannn amannn changed the title feat!: Remove default of now={new Date()} from NextIntlClientProvider for usage with format.relativeTime feat!: Remove default of now={new Date()} from NextIntlClientProvider for usage with format.relativeTime (preparation for dynamicIO) Nov 14, 2024
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.

1 participant