Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-76231

Rewrite core/ajax as an ESM using the fetch API

      The Fetch API is an interface for fetching resources across the network. It's essentially a replacement for the XMLHttpRequest API, with huge simplifications and using a first class Response and Request object.

      Our current implementation for fetchign data from a server in JS is core/ajax. This:

      • is written as an AMD module in define module format
      • uses jquery to perform all operations
      • only has a call function - used to fetch multiple requests in one go
      • support synchronous fetches (which block)
      • returns jQuery promises

      Whilst the XMLHttpRequest object, and jquery, will not be going anywhere any time soon, it would simplify a lot of our interactions to use the Fetch API. Benefits include:

      • returning native Promises, which are easier to debug and work with
      • supports aborting
      • supports keepalive (in Chrome, but not Firefox) as a replacement to the Navigator.sendBeacon API
      • a much simpler API
      • A much simpler Response object which is easier to work with
      • A whole load of options to make it easier to make changs

      This issue:

      • adds a new core/fetch module with functions:
        • fetchOne() - make a single API call with a simplified API
        • fetchMany() - similar to the core/ajax::call function but with simplified options
      • modified core/ajax to call core/fetch in a backwards-compatible fashion
      • deprecated the sync option - it should never be used and its use has been strongly discouraged for a very long time
      • deprecates the done and fail properties on the requests object passwed to ajax::call because:
        • these are counter-intuitive
        • they tie us to jQuery Promises more tightly
      • migrating a bunch of places which use core/ajax.call in central locations to use the new API (and to ESM at the same time)

      The core/ajax module is not deprecated as part of this change. I suggest that we consider doing so in the future, but I would recommend that we mark it as deprecated for Moodle 4.5 LTS.

            dobedobedoh Andrew Lyons
            dobedobedoh Andrew Lyons
            Votes:
            1 Vote for this issue
            Watchers:
            13 Start watching this issue

              Created:
              Updated:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 4 hours, 5 minutes
                4h 5m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.