|
| 1 | +--- |
| 2 | +title: The Fetch batcher |
| 3 | +tags: |
| 4 | + - Javascript |
| 5 | + - AJAX |
| 6 | + - Fetch |
| 7 | + - Routing |
| 8 | +--- |
| 9 | + |
| 10 | +<Since version="6.0" issueNumber="MDL-82652" /> |
| 11 | + |
| 12 | +The `core/fetch` module makes requests to the Moodle [REST API (routes)](../../../apis/subsystems/routing/index.md). In addition to immediate requests, it can queue several requests and send them as a single HTTP `$batch` request, reducing the number of round trips. |
| 13 | + |
| 14 | +## Immediate requests {/* #immediate-requests */} |
| 15 | + |
| 16 | +Static methods perform a single request immediately: |
| 17 | + |
| 18 | +```js |
| 19 | +import Fetch from 'core/fetch'; |
| 20 | + |
| 21 | +const response = await Fetch.performGet('mod_example', 'animals', {params: {type: 'mammal'}}); |
| 22 | +``` |
| 23 | + |
| 24 | +`performGet`, `performHead`, `performPost`, `performPut`, `performPatch` and `performDelete` are available. |
| 25 | + |
| 26 | +## Batching requests {/* #batching-requests */} |
| 27 | + |
| 28 | +The same methods are available on an _instance_. Instance calls are queued and sent together. |
| 29 | + |
| 30 | +### The automatic batcher {/* #the-automatic-batcher */} |
| 31 | + |
| 32 | +`Fetch.getBatcher()` returns a shared singleton which automatically executes the queue 50ms after the last request was added. |
| 33 | + |
| 34 | +```js |
| 35 | +import Fetch from 'core/fetch'; |
| 36 | + |
| 37 | +const batcher = Fetch.getBatcher(); |
| 38 | +const [mammals, reptiles] = await Promise.all([ |
| 39 | + batcher.performGet('mod_example', 'animals', {params: {type: 'mammal'}}), |
| 40 | + batcher.performGet('mod_example', 'animals', {params: {type: 'reptile'}}), |
| 41 | + batcher.performDelete('mod_example', `animals/${pig.id}`), |
| 42 | +]); |
| 43 | +``` |
| 44 | + |
| 45 | +Each call returns a `Promise` resolving to a standard `Response` for that sub-request, and failures reject only the individual promise. |
| 46 | + |
| 47 | +### A manual batcher {/* #a-manual-batcher */} |
| 48 | + |
| 49 | +Create an instance yourself and call `execute()` when ready. An optional constructor argument sets an auto-execute delay in milliseconds. |
| 50 | + |
| 51 | +```js |
| 52 | +const batcher = new Fetch(); |
| 53 | +const results = Promise.all([ |
| 54 | + batcher.performGet('mod_example', 'animals', {params: {type: 'mammal'}}), |
| 55 | + batcher.performGet('mod_example', 'animals', {params: {type: 'reptile'}}), |
| 56 | +]); |
| 57 | + |
| 58 | +batcher.execute(); |
| 59 | +await results; |
| 60 | +``` |
| 61 | + |
| 62 | +## Behaviour notes {/* #behaviour-notes */} |
| 63 | + |
| 64 | +- If a batch contains a single request, the batch endpoint is _not_ used; it is sent as a normal request. |
| 65 | +- The queue is flushed automatically once it holds more than 20 requests. |
| 66 | +- If the `$CFG->batch_fetch_requests` setting (JS: `M.cfg.batchFetchRequests`, default `true`) is disabled, each request is executed immediately instead of being queued. |
| 67 | + |
| 68 | +## Cacheable requests bypass the batcher {/* #cacheable-requests-bypass-the-batcher */} |
| 69 | + |
| 70 | +Requests made with a `cachekey` (see [route caching](../../../apis/subsystems/routing/caching.md)) are sent individually, even via a batcher. Hiding them inside a generic `$batch` request would prevent browsers and reverse proxies caching each URL. |
| 71 | + |
| 72 | +```js |
| 73 | +const response = await Fetch.getBatcher().performGet('core', 'strings/en/core', {cachekey: M.cfg.jsrev}); |
| 74 | +``` |
| 75 | + |
| 76 | +To batch these requests anyway, set `$CFG->batch_api_asset_requests = true` (JS: `M.cfg.batchApiAssetRequests`). |
| 77 | + |
| 78 | +## See also {/* #see-also */} |
| 79 | + |
| 80 | +- [AJAX](./index.md) |
| 81 | +- [Routing: Caching](../../../apis/subsystems/routing/caching.md) |
0 commit comments