Skip to content

React errors: update DateTime component to avoid calling Date.now() #14358

@jamesmockett

Description

@jamesmockett

The DateTime component calls Date.now() when rendered which is passed as a prop to RelativeTime to allow calculation of a relative timestamp. This assumes that the component is always rendered on the server, but we have cases where DateTime is rendered inside an island within a parent component. When the island is hydrated Date.now() is called again on the client and the value of the prop passed to RelativeTime may no longer match causing a hydration error.

The time is rounded down to the nearest minute so these errors may not always be apparent as the time on the server and client may still match. It can be replicated by opening a Front and leaving the page open for at least a minute before scrolling down. When islands containing DateTime further down the page are hydrated the time fetched on the client will have diverged from the server time when initially rendered, leading to a hydration error.

The solution is to lift up fetching the current time to a higher level so that it only happens once, and never on the client. This can then be passed down to instances of DateTime via prop drilling or context.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions