You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
<FormattedRelative /> React attempted to reuse markup in a container but the checksum was invalid #186
I'm trying to use <FormattedRelative> in a universal javascript environment and I'm not sure how to make this error go away:
Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
(client) 0.$0.0.1.2.1.1.1.0">8 seconds ago</span>
(server) 0.$0.0.1.2.1.1.1.0">5 seconds ago</span>
// Get `now` from somewhere…<FormattedRelativevalue={someDate}now={serverNow}/>
This requires you to send the now value to the client and make it accessible to your components when they render.
I'll have to think about a way to make this easier for people since many will hit this. Thanks for bringing it up, and let me know if you come up with a good way in the meantime to track "now" throughout your app during the initial rendering.
I'm wondering if this is something that should be supported on <IntlProvider> so it can automatically provide this initial "now" value to all <FormattedRelative> instances unless there's one already provided.
Probably, so would this mean that in a universal application context, the entire app's lifecycle would revolve around the serverNow? ie. at 12:00pm the page is loaded. Items display as 5 seconds ago. User browse to another page using client side routing. Then returns to the previous page. The items still display as 5 seconds ago.
I think there's two features here to considering building into React Intl:
A universal app's initial render on the client needs to use the same absolute reference time ("now") when formatting relative times: <FormattedRelative> elements and calls to formatRelative(). A universal app's server should be able to capture the time it begins rendering, provide that time as a prop to some root React component, and serialize it so the client can use the same reference time when it rehydrates the app.
Relative times rendered in an app should be "alive" and tick at some interval after the initial render. This would make sure relative dates in the UI are never stale. This "ticking" would cause a re-render, so ideally its isolated to only <FormattedRelative> instances so the whole component tree doesn't have to incur needless re-renders.
I think these two features are highly related. In your original examples you'd want both the server and client to render: "5 seconds ago", but after the client's initial render, the displayed time should update itself to render: "8 seconds ago".
👍 yes that would make sense. Well one way is to use a wrapper around FormattedRelative and supply serverNow only if time elapsed from clientNow is < 30 seconds or other value. This would then ensure all subsequent renders from the client side will keep using clientNow after 30 seconds.
This way we would not need to tick every interval and the timeago will be accurate on the client side after the elapsed time.
I think the "ticking" is a desired feature and should be the default. The trick will be to isolate the re-renders to only the <FormattedRelative> instances. I want this to work with many component instances on the page and be performant for n = 1000.
@ericf I would propose to add a shouldTick prop (name TBD) to FormattedRelative so that users have the ability to disable it if they want to. Not everybody needs the relative times to stay in sync. In addition to that I think we should implement a global setting on IntlProvider which allows to define the default shouldTick value for all FormattedRelatives so that we can disable ticking globally for all those components without having to pass in the prop everywhere. It should be overridable on a local level though. So if the default is false and the prop value is true the component should perform ticks.
This means that those components will have to be stateful though. In addition to the shouldTick prop I think a tickInterval prop makes sense, which should be globally or locally definable - just like shouldTick. This means - again - that the user is in full control regarding the re-renders of the component. If I only want to tick/re-render the component every 10 seconds I should be able to do so, right? Maybe I only need to tick some components every minute, but others every second. Being able to do that would give us more flexibility at a very low cost I think.
And just to be clear: the provided serverNow should only be used for the initial render, in order to prevent having to re-render completely on the client, is that correct? Every other render after the initial render is supposed to use the client's local time, right?
@johanneslumpe Thanks for providing these ideas, they are very much aligned with how I've been thinking about this. Replies inline…
I would propose to add a shouldTick prop (name TBD) to FormattedRelative so that users have the ability to disable it if they want to. Not everybody needs the relative times to stay in sync.
I disagree; I strongly feel the relative times should update by default so that the information they present stays up to date and is accurate. I think the main objection to this would be if the default had them updating every second, making it distracting in some contexts. Maybe the default periodicity is 60 seconds, but for a realtime app, like Twitter, they could update every second.
The auto updating would only be a feature of the <FormattedRelative> React component, the formatRelative() API could be used if auto-updating as not desired since it always returns a string value.
a global setting on IntlProvider which allows to define the default shouldTick value for all FormattedRelatives so that we can disable ticking globally for all those components without having to pass in the prop everywhere.
Yeah, I think this seems right. If what people are after is something much more advanced, like a back-off mechanism as the delta gets larger, or different update schedules for different parts of the app, then another <*Provider> component might be better.
This means that those components will have to be stateful though.
Yes, but I think that's okay.
If I only want to tick/re-render the component every 10 seconds I should be able to do so, right?
Yeah, but I want the user to think about the UX or product decision part of this problem, and declare to React Intl their choice and under the hook the mechanics of the actually updating in a performant way will be handled automatically.
Maybe I only need to tick some components every minute, but others every second. Being able to do that would give us more flexibility at a very low cost I think.
What I'm hoping to avoid is having the user specify a more granular schedule: 0–5 sec: update every second, 6–59 sec update every 10 seconds, 1–5 min update every minute, 6–59 min update every 5 mins, 1+ hour update every hour, etc.
And just to be clear: the provided serverNow should only be used for the initial render, in order to prevent having to re-render completely on the client, is that correct? Every other render after the initial render is supposed to use the client's local time, right?
Yup, that's what I'm thinking. Then if the "ticking" behavior is enabled, it would start the scheduler and the instances would begin displaying dates relative to the client's Date.now(). For this prop I would prefix it with initial*.
Activity
ericf commentedon Oct 15, 2015
You can use a stable "now" value.
Calculate "now" on the server and provide it to your app somehow:
Also provide this value to the client so it can access it:
`<script>window.serverNow = ${serverNow};</script>`On the server and client:
This requires you to send the
nowvalue to the client and make it accessible to your components when they render.I'll have to think about a way to make this easier for people since many will hit this. Thanks for bringing it up, and let me know if you come up with a good way in the meantime to track "now" throughout your app during the initial rendering.
geekyme commentedon Oct 15, 2015
but
nowwill get calculated once on the server side when rendering on the server side, and then again on the client side?ericf commentedon Oct 15, 2015
@geekyme I updated my comment to make it more clear.
geekyme commentedon Oct 15, 2015
ok let me try this
ericf commentedon Oct 15, 2015
I'm wondering if this is something that should be supported on
<IntlProvider>so it can automatically provide this initial "now" value to all<FormattedRelative>instances unless there's one already provided.initialNowprobably isn't the best prop name, but you get the idea…geekyme commentedon Oct 15, 2015
Probably, so would this mean that in a universal application context, the entire app's lifecycle would revolve around the
serverNow? ie. at 12:00pm the page is loaded. Items display as 5 seconds ago. User browse to another page using client side routing. Then returns to the previous page. The items still display as 5 seconds ago.That's a little weird.
geekyme commentedon Oct 15, 2015
For Fluxible users that might stumble on this problem, I believe we can declare a context plugin that a wrapper around
FormattedRelativecould use. The plugins allows you to create theserverNowand then dehydrate to the client side for usage. http://fluxible.io/api/plugins.html#creating-pluginsericf commentedon Oct 15, 2015
I think there's two features here to considering building into React Intl:
<FormattedRelative>elements and calls toformatRelative(). A universal app's server should be able to capture the time it begins rendering, provide that time as a prop to some root React component, and serialize it so the client can use the same reference time when it rehydrates the app.<FormattedRelative>instances so the whole component tree doesn't have to incur needless re-renders.I think these two features are highly related. In your original examples you'd want both the server and client to render: "5 seconds ago", but after the client's initial render, the displayed time should update itself to render: "8 seconds ago".
geekyme commentedon Oct 15, 2015
👍 yes that would make sense. Well one way is to use a wrapper around
FormattedRelativeand supplyserverNowonly if time elapsed fromclientNowis < 30 seconds or other value. This would then ensure all subsequent renders from the client side will keep usingclientNowafter 30 seconds.This way we would not need to tick every interval and the timeago will be accurate on the client side after the elapsed time.
the context
serverNowis provided by my application through dehydrating theserverNowon the server side. its easy to do this with fluxible using:geekyme commentedon Oct 15, 2015
My only worry is whether doing all these comparisons will be expensive. Especially there are about 50-100 items using
<FormattedRelative>on my screenericf commentedon Oct 15, 2015
I think the "ticking" is a desired feature and should be the default. The trick will be to isolate the re-renders to only the
<FormattedRelative>instances. I want this to work with many component instances on the page and be performant for n = 1000.geekyme commentedon Oct 17, 2015
👍 looking forward to the next pr
johanneslumpe commentedon Oct 17, 2015
@ericf I would propose to add a
shouldTickprop (name TBD) toFormattedRelativeso that users have the ability to disable it if they want to. Not everybody needs the relative times to stay in sync. In addition to that I think we should implement a global setting onIntlProviderwhich allows to define the defaultshouldTickvalue for allFormattedRelatives so that we can disable ticking globally for all those components without having to pass in the prop everywhere. It should be overridable on a local level though. So if the default isfalseand the prop value istruethe component should perform ticks.This means that those components will have to be stateful though. In addition to the
shouldTickprop I think atickIntervalprop makes sense, which should be globally or locally definable - just likeshouldTick. This means - again - that the user is in full control regarding the re-renders of the component. If I only want to tick/re-render the component every 10 seconds I should be able to do so, right? Maybe I only need to tick some components every minute, but others every second. Being able to do that would give us more flexibility at a very low cost I think.And just to be clear: the provided
serverNowshould only be used for the initial render, in order to prevent having to re-render completely on the client, is that correct? Every other render after the initial render is supposed to use the client's local time, right?ericf commentedon Oct 19, 2015
@johanneslumpe Thanks for providing these ideas, they are very much aligned with how I've been thinking about this. Replies inline…
I disagree; I strongly feel the relative times should update by default so that the information they present stays up to date and is accurate. I think the main objection to this would be if the default had them updating every second, making it distracting in some contexts. Maybe the default periodicity is 60 seconds, but for a realtime app, like Twitter, they could update every second.
The auto updating would only be a feature of the
<FormattedRelative>React component, theformatRelative()API could be used if auto-updating as not desired since it always returns a string value.Yeah, I think this seems right. If what people are after is something much more advanced, like a back-off mechanism as the delta gets larger, or different update schedules for different parts of the app, then another
<*Provider>component might be better.Yes, but I think that's okay.
Yeah, but I want the user to think about the UX or product decision part of this problem, and declare to React Intl their choice and under the hook the mechanics of the actually updating in a performant way will be handled automatically.
What I'm hoping to avoid is having the user specify a more granular schedule: 0–5 sec: update every second, 6–59 sec update every 10 seconds, 1–5 min update every minute, 6–59 min update every 5 mins, 1+ hour update every hour, etc.
Yup, that's what I'm thinking. Then if the "ticking" behavior is enabled, it would start the scheduler and the instances would begin displaying dates relative to the client's
Date.now(). For this prop I would prefix it withinitial*.ericf commentedon Oct 19, 2015
I dug around in React's issues to see if there were any discussions on time and timers and found:
Stabilize "now" time and "tick" relative times
ericf commentedon Oct 21, 2015
@geekyme @johanneslumpe please review #191 if you have time. Thanks!