markdalgleish / redial

Universal data fetching and route lifecycle management for React etc.
1.1k stars 42 forks source link

in example client code should defer hooks be called after fetch? #39

Closed modosc closed 4 years ago

modosc commented 8 years ago

from the client example:

      if (window.INITIAL_STATE) {
        delete window.INITIAL_STATE;
      } else {
        trigger('fetch', components, locals);
      }

      trigger('defer', components, locals);

to me it makes more sense to wait on the defer hooks until after the fetch hooks resolve - this way both the client and server renders behave the same:

      var fetch
      if (window.INITIAL_STATE) {
        delete window.INITIAL_STATE;
        fetch = Promise.resolve(true);
      } else {
        fetch = trigger('fetch', components, locals);
      }

      fetch.then(() => { return trigger('defer', components, locals); })

i realize i'm free to do this on my own (and i have), just wondering if this was intentional or not.

thanks!

kikoanis commented 8 years ago

Not sure if this is really relevant to the library but rather an implementation detail. If you think that the defer actions have to wait for data that should be retrieved by the fetch actions then yeah, your proposal is correct. In my experience, most defer actions are not related directly to the main fetch actions but rather for different components that the router handler tries to render and hence triggering it on the client right after the fetch actions (if not first time) makes your rendering more robust and fast.

modosc commented 8 years ago

i only mention it because we ran into some issues with it. since this boilerplate is copied all over the place it would be nice to either change it to something the above or at least make a note since it's a subtle bug.