TimeField component and hydration error (Next.js)
Provide a general summary of the issue here
The TimeFied component triggers a hydration error in Safari and Firefox, but seems to work correctly in Chrome.
๐ค Expected Behavior?
No hydration error expected
๐ฏ Current Behavior
๐ Possible Solution
No response
๐ฆ Context
No response
๐ฅ๏ธ Steps to Reproduce
Repo: https://github.com/pasqualevitiello/rac-timefield-issue Demo link: https://rac-timefield-issue.vercel.app/
Version
rac 1.4.0
What browsers are you seeing the problem on?
Firefox, Safari
If other, please specify.
No response
What operating system are you using?
macOS 15
๐งข Your Company/Team
No response
๐ท Tracking Issue
No response
This is caused by the format of the date on the node server and within Firefox/Safari being different. It could be fixed with the suppressHydrationWarning property. Other fixes include using useEffect to hydrate a component or create the first instance of a date in the browser.
Thanks! Not sure to understand how to use suppressHydrationWarning. Adding the attribute to any of the components does not silence the warning.
It goes directly on the element, not a prop on the React Aria component (currently).
I feel stupid for not figuring out how to resolve the issue. Would you mind to provide an example based on this component?
I believe what is being mentioned above would need to be implemented on our end since it needs to be set on the div/input/something rendered by the TimeField component internally. That being said, https://rac-timefield-issue.vercel.app/ doesn't seem to error for me in Safari or Firefox.
Cool, thanks!
I also have this problem, the diff is basically a different kind of space character. Thanks to Next.js 15.2, we have a better view of the hydration errors.
+ {" "}
- {"โฏ"}
As mentioned, I can't use the suppressHydrationWarning prop directly. This happens on Firefox and Safari, not on Chrome, I can confirm. Can this be reopened?
It appears that this is an issue in node, where a NARROW NO-BREAK SPACE is used instead of regular space.
const formatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'long' });
const segments = formatter.formatToParts(new Date())
const secondsSegmentIndex = segments.findIndex(segment => segment.type === 'second');
console.log(segments[secondsSegmentIndex+1].value.charCodeAt(0));
The above prints 8239 in node, but 32 in browsers.
This sounds related to https://github.com/nodejs/help/issues/4068, which was fixed, but the fix apparently was only applied to date.toLocaleString and formatter.format, not to formatter.formatToParts.
TimeField is using useDateFieldState, which is using DateFormatter from @internationalized/date
DateFormatter is a wrapper around node's Intl.DateTimeFormat and passes formatToParts calls to the original implementation: https://github.com/adobe/react-spectrum/blob/main/packages/%40internationalized/date/src/DateFormatter.ts#L37
The best way to fix this is to fix node, but patching DateFormatter to replace the NARROW NO-BREAK SPACE with a regular space is also an option.
Thanks for the investigation! I've re-added this issue to our board for triage and grooming, but definitely would accept a contribution!
If we change it in our component, won't it work in FF and Safari and break in Chrome instead of the other way around? I think the only way this gets fixed is if all the js environments agree.
It's possibly in a similar vein as https://github.com/adobe/react-spectrum/issues/8503, in that if we knew the user agent we could "do the right thing", however, that seems brittle and would just break again if/when the browsers change their implementation.
It also requires people to pass in the user agent and know that they have to do that for it to work.
If we change it in our component, won't it work in FF and Safari and break in Chrome instead of the other way around? I think the only way this gets fixed is if all the js environments agree.
If you decide to go with a fix in your component, you could implement it in a non-breaking way for Chromium-based browsers. If the fix simply normalizes the return of DateFormatter.formatToParts, replacing NARROW NO-BREAK SPACE with a regular space, it will make the component render with regular spaces in Node AND all of the browsers. With that, there should be no hydration errors regardless of your browser or node version.
I fully understand that a fix in V8 is preferred, as that would eventually make its way into both Node and Chromium, but it might take a while. A temporary fix in @internationalized/date would be faster and can eventually be removed once stable Chromium and Node get shipped with the real fix.
I'll try to come up with a PR to update @internationalized/date DateFormatter this week.
Ah, yep, I see what you're saying. I over thought it. Thanks.
I have submitted a PR with the character normalization logic. https://github.com/adobe/react-spectrum/pull/8826
With that, AM/PM should always be preceded with a regular space regardless of the runtime environment.
I got some more information on the direction this is headed in browsers https://github.com/tc39/ecma402/issues/1028#issuecomment-3305090510
An interesting suggestion from that is to use ICU4X. Because we are a library, we do not want people who are not using SSR to pay the price of this additional dependency. But perhaps through our I18nProvider we could allow people to specify a different implementation of Intl.
Something like
function I18nProvider(props) {
let ctxIntl = props.Intl ?? Intl;
return <I18nContext.Provider ctxIntl...
}
and then anywhere we use Intl, we grab it from the context instead.
We have some non hook and non component functions which also make use of Intl, we'd need to add additional optional args to all of those. Such as in https://github.com/adobe/react-spectrum/blob/6e467f870a596c82014895934f4e60834cba0550/packages/%40internationalized/date/src/DateFormatter.ts#L135
I also have this problem, the diff is basically a different kind of space character. Thanks to Next.js 15.2, we have a better view of the hydration errors.
+ {" "} - {"โฏ"}As mentioned, I can't use the
suppressHydrationWarningprop directly. This happens on Firefox and Safari, not on Chrome, I can confirm. Can this be reopened?
I have the exact same issue @frontsideair , how did you fix it?
(
<AriaTimeField
hideTimeZone
aria-label="Pick a time"
defaultValue={props.defaultValue}
className={cx(
"flex rounded-lg bg-primary px-2.5 py-2 text-md shadow-xs ring-1 ring-primary ring-inset focus-within:ring-2 focus-within:ring-brand",
typeof props.className === "string" && props.className,
)}
isDisabled={isDisabled}
{...props}
>
<DateInput>
{(segment) => <DateSegment segment={segment}
className={cx(
"rounded px-px text-primary tabular-nums caret-transparent focus:bg-brand-solid focus:font-medium focus:text-white focus:outline-hidden",
// The placeholder segment.
segment.isPlaceholder && "text-placeholder uppercase",
// The separator "/" segment.
segment.type === "literal" && "text-fg-quaternary",
)}
/>}
</DateInput>
</AriaTimeField>
)
i'm on [email protected], [email protected], @internationalized/[email protected] and node 22.17
@benjyAuthi The most straightforward thing you can do is only render the component on the client, rendering a skeleton on the server.
If you absolutely must render the element on the server, you could try and manually normalize the space character as you pass segment to DateSegment. I haven't tried that approach, but it might just work for you, especially if you only care about English. If your scope is broader than English, you'll never catch a break as discrepancies between node and browsers go beyond just space characters for a lot of locales.
With the latest release and the render prop you can prevent the issue as below.
DateSegment itself does not pass through suppressHydrationWarning
I think it is fine if react does not patch the different whitespace character.
<DateSegment
segment={segment}
render={(props) => (
<span {...props} suppressHydrationWarning />
)}
/>
We have seen similar errors with different hyphens we formatting numbers or dates, depending on if the component is server-rendered or not. With hyphens, you likely want react not to patch the diff, otherwise your interface can jump depending in the hyphen (or in the above case spacing) width.
Alternative solution that could be implemented within the lib is using useSyncExternalStore for getting a client and server snapshot, both could call the same function and even though outputs are different on client and server react would not give an hydration error - as this is expected.
It would work both in hybrid client/server and client only applications.
But it might be overkill here.