Skip to content

XHR option to trigger busy indicators #19

Closed

Description

@stevesouders

Browsers have default "busy indicators" that provide feedback to users that the page is loading, eg, the tab icon, status bar, and reload icon. These busy indicators are triggered in some situations (eg, clicking a link), but not others (eg, issuing an XHR). (See http://www.stevesouders.com/blog/2013/06/16/browser-busy-indicators/.)

In the absence of these busy indicators, users are uncertain and thus anxious about the status of pages loading. If the XHR response is slow to arrive and its content is critical to the user experience, the user has no idea that anything is happening. This problem grows as more pages adopt XHRs for core functionality.

A good example is this article from eBay about how their XHR-based single-page-web-app was perceived as slow until they added a progress bar (http://calendar.perfplanet.com/2014/the-power-of-perceived-performance/). That article also mentions Twitter's approach of adding a throbber to the page when XHRs are fetching tweets.

While requiring developers to add custom progress indicators is a workaround to this problem, it's not ideal because 1) it's more work for developers and 2) each progress indicator is a custom implementation and thus there is not a typical experience for the user that they are trained to look for and expect.

Since users are "trained" to look for the typical busy indicators, these indicators should be triggered when an XHR is issued for content the user is waiting on. However, XHRs are also used for background requests where the busy indicators should NOT be triggered. I suggest we add an option to XMLHttpRequest, eg, showBusy. Setting showBusy=true would explicitly tell the browser to trigger the busy indicators. This would allow for the current behavior (no busy indicators) to be preserved, but also allows an easier and more standard way for developers to use XHR while giving progress feedback to users.

Activity

  1. annevk commented on Jan 13, 2015

    @annevk
    Member

    Is there implementer interest to the extent they are willing to put UX resources on this? (Opera used to show all XMLHttpRequest activity and it was very bad (due to the background resources you mention).)

  2. josh commented on Jan 19, 2015

    @josh

    I would ❤️ this so much and we'd totally use this on @github for pretty much all our page transitions instead of custom spinners.

  3. jussi-kalliokoski commented on Jan 19, 2015

    @jussi-kalliokoski

    👍, but instead I'd suggest programmatic control over the feature, because loading may include JS, fonts, images or processing in a worker (e.g. if a data blob is cached, but requires processing before being fed to a view).

  4. jussi-kalliokoski commented on Jan 19, 2015

    @jussi-kalliokoski

    And also because the actions may be canceled (e.g. a user clicks on a wrong link and then the correct one).

  5. d2s commented on Jan 19, 2015

    @d2s

    More consistent UI behaviour between websites (and different browsers…) could make things much more clear for the users. Also, less code to implement for front-end developers, reducing need for additional building blocks.

  6. rauchg commented on Jan 19, 2015

    @rauchg

    This should not be tied into XMLHttpRequest, because of possible alternative transports in play (WebSocket). Also because a sequence of requests might be needed to trigger the following page.

    Maybe we should consider something on the navigator or window object.

    navigator.setBusy(true);

    I personally would like to see this happen not just because of the perception of slowness, but because of the duplication of user-agent UI that's happening with the "slim progress bar" trend (as seen on YouTube, Medium, etc)

    The performance angle is more nuanced. Many single page applications solve it by making the transition to the new page immediate, and displaying placeholders there (eg: Facebook Newsfeed)

  7. josh commented on Jan 19, 2015

    @josh

    It's more than a binary state, you really want something associated with
    progress events.

    I kinda doubt UAs are going to give complete direct access to something
    like navigator.setProgress(0.75) given the ability for abuse, even though
    it'd be pretty cool.

    Restricting to something request-like would cover most use cases.
    On Mon, Jan 19, 2015 at 12:33 AM Guillermo Rauch notifications@github.com
    wrote:

    This should not be tied into XMLHttpRequest, because of possible
    alternative transports in play (WebSocket). Also because a sequence of
    requests might be needed to trigger the following page.

    Maybe we should consider something on the navigator or window object.

    navigator.setBusy(true);

    I personally would like to see this happen not just because of the
    perception of slowness, but because of the duplication of user-agent UI
    that's happening with the "slim progress bar
    https://github.com/rstacruz/nprogress" trend (as seen on YouTube,
    Medium, etc)

    The performance angle is more nuanced. Many single page applications solve
    it by making the transition to the new page immediate
    http://rauchg.com/2014/7-principles-of-rich-web-applications/#act-immediately-on-user-input,
    and displaying placeholders there (eg: Facebook Newsfeed)

    —
    Reply to this email directly or view it on GitHub
    #19 (comment).

  8. rauchg commented on Jan 19, 2015

    @rauchg

    It depends on how it's implemented. The iOS status bar binary loading state works pretty well, for example.

  9. josh commented on Jan 19, 2015

    @josh

    Also consider that the "Stop" button is closely linked to "in progress".
    It'd be kinda neat if that put the user in control of canceling the request
    if they wish. Shout to cancelable fetch promises.
    On Mon, Jan 19, 2015 at 1:22 AM Guillermo Rauch notifications@github.com
    wrote:

    It depends on how it's implemented. The iOS status bar binary loading
    state works pretty well, for example.

    —
    Reply to this email directly or view it on GitHub
    #19 (comment).

  10. gaearon commented on Jan 19, 2015

    @gaearon

    navigator.setBusy(true);

    Different parts of code may want to push/pop an async operation so this wouldn't work well as the primary user-facing API IMO.

    Instead I'd rather prefer something like this:

    window.attachToBusyIndicator(promise);

    Internally, we'd keep track of promises in progress and only show spinner when there is >= 1 such promise. When promises resolve, we remove them from the queue.

    var busyPromises = [];
    
    function refresh() {
      NativeImplementation.showBusyIndicator = busyPromises.length > 0;
    }
    
    window.attachToBusyIndicator = function (promise) {
      function detach() {
        busyPromises.splice(busyPromises.indexOf(promise), 1);
        refresh();
      }
    
      promise.then(detach, detach);
      busyPromises.push(promise);
    
      refresh();
    }

    This also works nice with cancellation because, whatever approach you choose for cancelling promises, if cancellation implies rejection (and it should), our counter will decrement on cancellation. Example of a nice cancellation API.

  11. fabiosantoscode commented on Jan 19, 2015

    @fabiosantoscode

    I completely agree that the browser's busy indicator should not be tied to XHR or fetch(). What about JSONP, WebSockets, PeerConnections? they are perfectly valid ways of getting data which you have to wait on.

    Busy indicators, progress indicators, are visual indicators, and thus in principle should not be triggered by network operations, even as an opt-in.

  12. rauchg commented on Jan 19, 2015

    @rauchg

    @gaearon I actually prefer browser APIs to be minimal. You can add the Promise layer on top of it trivially. Just like not everyone uses XHR exclusively (the very premise of my post), not everyone has to use Promise to trigger the indicator.

  13. gaearon commented on Jan 19, 2015

    @gaearon

    @rauchg

    I was thinking about bad libraries messing with a boolean field that you have no control over. Imagine some SDK doing that. Maybe it's a library's problem though.. I'm not sure it's good to encourage bad patterns by exposing API that's too easy to misuse.

    And for a good API, I can't think of a better fit than promise. It represents an asynchronous operation in the language. You don't need to use promises in your code to represent async operations if you don't want to, but wrapping any async result (socket, XHR, whatever) in a promise is as straightforward as any other async API could be. If you want to track async operations in the UI, you need a way to represent an async value, and promises are baked into the language precisely to be the way to do that.

    If core browser APIs expose promise-based async operations such as fetch, IMO it's weird for async operation tracker to expose lower or higher level API. Otherwise, why make fetch promise-based?

    Finally, I can't build promise-based layer on top of isBusy because I have no guarantees somebody (including third-party libraries) doesn't set isBusy directly, thus making my layer completely unreliable.

  14. rauchg commented on Jan 19, 2015

    @rauchg

    @gaearon

    Your layer can be implemented by listening to busyChange events and querying an isBusy flag.
    That said, I agree that Promise could increase harmony between 3rd party libraries.

  15. gaearon commented on Jan 19, 2015

    @gaearon

    @rauchg

    Your layer can be implemented by listening to busyChange events and querying an isBusy flag.

    Say I have two pending promises in my layer. Thus I set isBusy = true.

    Then nasty module X comes along and does .isBusy = true.
    But it's busy already. Would the busychange event fire?

    If it's a change event, it wouldn't, but then, when my pending promises have resolved, I'd have no way to know from my layer that somebody else wanted to keep the app busy.

    If we're sure we don't want a Promise API for that, I still suggest two methods over isBusy:

    window.setBusyIndicator(): operationId
    window.clearBusyIndicator(operationId)

    These can be called safely from different modules.

  16. 21 remaining items

  17. duanyao commented on May 15, 2015

    @duanyao

    Also, note that webapps already implement their own progress indicators, and if XHR isn't handled properly, those can spin indefinitely too.

    This is why custom progress indicator is hard to get right. This proposal is supposed to address this issue, right? If you give developer a tool looks very simple, but actually difficult to use correctly, the result is not ideal.

    Managing the state of busy indicator explicitly is actually very hard.

    • Overlapping XHRs are hard to handle. You can't simply turn the indicator on when starting a XHR, and turn it off when the XHR finished or failed; instead, you need a global counter of pending XHRs. This can be impractical if a app contains many modules maintained by individual parties.
    • You have to manage the indicator(or the counter) at every places you call XHR, this is a big burden. If you are on a large code base, and someone makes a mistake when handling the indicator, it becomes a nightmare of debugging. Maybe you can write a nice wrapper for XHR/fetch that automatically manages the indicator, you still have to enforce the whole team to use it everywhere -- but how about third party modules?

    So, I don't believe average developers can get explicit busy indicator management right easily, not to mention those who copy-paste code snippets from arbitrary sites.

  18. Manishearth commented on May 15, 2015

    @Manishearth

    If you give developer a tool looks very simple, but actually difficult to use correctly, the result is not ideal.

    But the tool isn't the problem (the problem being "custom progress indicators get messed up if not done right") here, it's what the devs want to do -- and that's not changing.

    Overlapping XHRs are hard to handle.

    Again, already a problem, if the user does something to trigger multiple XHRs at once, a site not designed to handle it will stumble.

    Most of the problems you list are problems with giving devs the XHR API; not with a busy indicator.

  19. duanyao commented on May 15, 2015

    @duanyao

    Most of the problems you list are problems with giving devs the XHR API; not with a busy indicator.

    It is not meaningful to blame XHR or developers here, because even the brand new Fetch API won't make busy indicator easier to handle.

    The problem is the explicit state management. UAs already do good jobs at managing the state of busy indicator, why not reuse them? So I suggested FetchObserver and .affectBusyIndicator, and I believe this solution is hard to get wrong even if you copy-paste other's code blindly. FetchObserver also opens a door to many possibilities.

  20. smaug---- commented on Jul 5, 2015

    @smaug----

    In Gecko one can use a dummy iframe for this. iframe.contentDocument.open(); starts the busy indicator, and iframe.contentDocument.close() would stop it.
    I assume other engines would have the same behavior if they followed the spec on document.open()/close() more closely.

    But anyhow, I don't object adding API for busy indicator. Fetch spec of course wouldn't be the right place, but probably HTML.

  21. annevk commented on Jul 6, 2015

    @annevk
    Member

    It seems there is some interest from at least Mozilla in exposing this. I think the promise API proposed by @gaearon is the most promising. Maybe the constructor approach from @bloodyowl minus the progress bits (boolean seems fine given OS indicators to date). Though looking at https://w3c.github.io/wake-lock/ I wonder why @marcoscaceres ended up with just a property as API.

    whatwg/fetch is probably not the best place to hash this out. If some people here would like to turn this into something I can create whatwg/busy so there's a better place to evolve this. Any takers?

    (@duanyao you might be interested in #65. I don't think we want to tie a Busy Indicator API to fetching though. There's a number of other things such as WebSocket and WebRTC that are network-related and might cause a page to be busy. And I suppose we could even use it for non-network things.)

  22. realityking commented on Jul 6, 2015

    @realityking

    First of all, great news.

    Promises are not a bad idea, but I can think of at least one use case only badly served with promises:

    We're using angular-ui/ui-router in a number of Angular apps. Now if the library itself adds support, promises are easy as it works internally with (Angular-)Promises. However as a library user, all I have are the state change events which map relativly poorly to promises.

  23. domenic commented on Jul 6, 2015

    @domenic
    Member

    It's not a problem to adapt. addBusyPromise(new Promise(r => $rootScope.on('stateChangeStart', resolve))) is not much better or worse than var busyThing = new BusyThing(); busyThing.start(); $rootScope.on('stateChangeStart', () => busyThing.stop()).

  24. realityking commented on Jul 6, 2015

    @realityking

    Well it's tad bit more complicated, but you're right, it's not as bad as I though it would be:

    $rootScope.$on('$stateChangeStart', function() {
        addBusyPromise(new Promise(resolve => {
            $rootScope.on('$viewContentLoaded', resolve);
            $rootScope.on('$stateChangeError', resolve);
        }));
    });
  25. duanyao commented on Jul 7, 2015

    @duanyao

    @annevk Now I also agree on the promise API proposed by @gaearon, and am glad to hear about #65. Thanks!

  26. igrigorik commented on Jul 7, 2015

    @igrigorik
    Member

    What happens if I have composite tasks with variable weighting? E.g. I have tasks to download and process and image, and I want to assign a higher weight to download stage because I know that (in absolute time) that will take much longer? How do I script that with above promise API?

    Note that Apple recently revamped their API for progress indicators: https://developer.apple.com/videos/wwdc/2015/?id=232 - lots of good examples to think through there.

    On a slightly different note..

    • Not all UA's have a progress bar indicator, some use indeterminate indicators only
    • If we expose an API we should make both progress and indeterminate cases possible
    • We need to think through how such indicators overlap with browser triggered events...
      • Is this a separate indicator from the one browser uses?
      • Can/should the developer be able to control the browser indicator? E.g. call stop(), or some such, during page load to hide the indicator? (Not as crazy as it sounds)
      • Do the current browser indicators even make sense? e.g. onload is an anti-pattern.

    Related, but on-topic rant: https://www.igvita.com/2015/06/25/browser-progress-bar-is-an-anti-pattern/

    p.s. https://code.google.com/p/chromium/issues/detail?id=464377

  27. domenic commented on Jul 8, 2015

    @domenic
    Member

    This is a boolean progress indicator (the loading spinner), not anything with weighting or percentages.

  28. sicking commented on Aug 3, 2015

    @sicking

    I think so far people have mainly talked about the ability to turn on the progress indicator when the browser normally have it turned off.

    Do we also need a way for the page to turn off the progress indicator once it has loaded enough stuff that the page "is ready"? See the paragraphs after the image in @igrigorik post.

  29. annevk commented on Nov 12, 2015

    @annevk
    Member

    This is now whatwg/html#330. Thank you for the suggestion @stevesouders!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions