Skip to content

<FormattedRelative /> React attempted to reuse markup in a container but the checksum was invalid #186

Closed

Description

@geekyme

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>

Activity

  1. ericf commented on Oct 15, 2015

    @ericf
    Collaborator

    You can use a stable "now" value.

    Calculate "now" on the server and provide it to your app somehow:

    global.serverNow = Date.now();

    Also provide this value to the client so it can access it:

    `<script>window.serverNow = ${serverNow};</script>`

    On the server and client:

    // Get `now` from somewhere…
    <FormattedRelative value={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.

  2. geekyme commented on Oct 15, 2015

    @geekyme
    Author

    but now will get calculated once on the server side when rendering on the server side, and then again on the client side?

  3. ericf commented on Oct 15, 2015

    @ericf
    Collaborator

    @geekyme I updated my comment to make it more clear.

  4. geekyme commented on Oct 15, 2015

    @geekyme
    Author

    ok let me try this

  5. ericf commented on Oct 15, 2015

    @ericf
    Collaborator

    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.

    <IntlProvider locale="en-US" initialNow={window.serverTimeAtRender}>
        <FormattedRelative value={someDate} />
    </IntlProvider>

    initialNow probably isn't the best prop name, but you get the idea…

  6. geekyme commented on Oct 15, 2015

    @geekyme
    Author

    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.

  7. geekyme commented on Oct 15, 2015

    @geekyme
    Author

    For Fluxible users that might stumble on this problem, I believe we can declare a context plugin that a wrapper around FormattedRelative could use. The plugins allows you to create the serverNow and then dehydrate to the client side for usage. http://fluxible.io/api/plugins.html#creating-plugins

  8. ericf commented on Oct 15, 2015

    @ericf
    Collaborator

    I think there's two features here to considering building into React Intl:

    1. 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.
    2. 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".

  9. geekyme commented on Oct 15, 2015

    @geekyme
    Author

    👍 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.

    if (Component.displayName === "FormattedRelative") {
            const clientNow = Date.now();
            const serverNow = this.context.serverNow;
            propsOverride.now = clientNow - serverNow > 30000 ? clientNow : serverNow;
          }
    

    the context serverNow is provided by my application through dehydrating the serverNow on the server side. its easy to do this with fluxible using:

    function serverNowPlugin() {
      return {
        name: "ServerNowPlugin",
        plugContext: function(options) {
          let now = Date.now();
    
          return {
            plugComponentContext: function(componentContext) {
              componentContext.serverNow = now;
            },
    
            dehydrate: function() {
              return {
                serverNow: now
              }
            },
    
            rehydrate: function(state) {
              now = state.serverNow;
            }
          };
        }
      };
    }
    
    export default serverNowPlugin;
    
  10. geekyme commented on Oct 15, 2015

    @geekyme
    Author

    My only worry is whether doing all these comparisons will be expensive. Especially there are about 50-100 items using <FormattedRelative> on my screen

  11. ericf commented on Oct 15, 2015

    @ericf
    Collaborator

    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.

  12. geekyme commented on Oct 17, 2015

    @geekyme
    Author

    👍 looking forward to the next pr

  13. johanneslumpe commented on Oct 17, 2015

    @johanneslumpe

    @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?

  14. ericf commented on Oct 19, 2015

    @ericf
    Collaborator

    @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*.

  15. ericf commented on Oct 19, 2015

    @ericf
    Collaborator
  16. added a commit that references this issue on Oct 21, 2015
    567790d
  17. ericf commented on Oct 21, 2015

    @ericf
    Collaborator

    @geekyme @johanneslumpe please review #191 if you have time. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions