Ming-Ying Chung | 141600c | 2023-11-14 03:12:41 | [diff] [blame^] | 1 | # fecthLater API |
| 2 | |
| 3 | - Contact: pending-beacon-experiment@chromium.org |
| 4 | - API Feedback: https://github.com/WICG/pending-beacon/issues |
| 5 | |
| 6 | This document describes the status of the current implementation of the |
| 7 | [**FetchLater API**][spec-pr] in Chrome, and how to enable it manually. |
| 8 | |
| 9 | Starting from [version 120][status], Chrome experimentally supports the |
| 10 | FetchLater API, |
| 11 | which allows website authors to specify one or more beacons (HTTPS requests) |
| 12 | that should be sent reliably when the page is being unloaded. |
| 13 | |
| 14 | See the [public explainer][explainer] to learn more about how it works. |
| 15 | |
| 16 | Note that this API is not enabled by default. Instead, Chrome plans to run |
| 17 | Origin Trials to evaluate its impact. But Chrome also provides some ways to |
| 18 | fully opt-in to the API for web developers who what to try the features. |
| 19 | |
| 20 | [spec-pr]: https://github.com/whatwg/fetch/pull/1647 |
| 21 | [explainer]: https://github.com/WICG/pending-beacon/blob/main/docs/fetch-later-api.md |
| 22 | [status]: https://chromestatus.com/feature/4654499737632768 |
| 23 | |
| 24 | ## What’s supported |
| 25 | |
| 26 | Chrome supports all the JavaScript APIs described in the spec PR, |
| 27 | specifically: |
| 28 | |
| 29 | - [`fetchLater()`](https://whatpr.org/fetch/1647/9ca4bda...37a66c9.html#dom-global-fetch-later) |
| 30 | method, a `fetch()`-like API. |
| 31 | - [`Deferred fetching`](https://whatpr.org/fetch/1647/9ca4bda...37a66c9.html#deferred-fetching) |
| 32 | behavior. |
| 33 | |
| 34 | ## What’s not supported |
| 35 | |
| 36 | The following features are not finalized in the spec PR, and hence not supported |
| 37 | in Chrome: |
| 38 | |
| 39 | - The API is only supported in Document context. Supporting in ServiceWorker is |
| 40 | blocked by ServiceWorker version of https://crbug.com/1356128. |
| 41 | - Crash recovery related behaviors is not supported. |
| 42 | (Discussed in [pending-beacon/issues/34](https://github.com/WICG/pending-beacon/issues/34)) |
| 43 | - Retry after network failure is not supported. |
| 44 | (Discussed in [pending-beacon-/issues/40](https://github.com/WICG/pending-beacon/issues/40)) |
| 45 | - fetchLater requests are not observable in Chrome DevTools. |
| 46 | |
| 47 | The following features work differently than the one described in explainer and |
| 48 | spec: |
| 49 | |
| 50 | - To address the privacy requirement (see |
| 51 | [pending-beacon/issues/30](https://github.com/WICG/pending-beacon/issues/30#issuecomment-1333869614)), |
| 52 | fetchLater requests can only be sent (staying pending) after the page becomes |
| 53 | inactive, i.e. BFCached, if BackgroundSync permission is on for the Origin; |
| 54 | otherwise, all deferred and not-yet-active fetchLater requests will be forced |
| 55 | sending out on navigating away. |
| 56 | - A fetchLater request max TTL is bound by Chrome's back/forward cache TTL, |
| 57 | which is currently **10 minutes** after page goes into the cache. |
| 58 | |
| 59 | ## Activation |
| 60 | |
| 61 | The API can be enabled by a command line flag. |
| 62 | |
| 63 | ### Using command line flag |
| 64 | |
| 65 | Passing `--enable-features=FetchLaterAPI --enable-blink-features=FetchLaterAPI` |
| 66 | command line flag to Chrome enables FetchLater API support. |
| 67 | |
| 68 | ### Verifying the API is working |
| 69 | |
| 70 | Added the following line to an HTTPS web page, and load the page into a Chrome |
| 71 | tab. |
| 72 | |
| 73 | ```html |
| 74 | <script> |
| 75 | fetchLater('/test'); |
| 76 | </script> |
| 77 | ``` |
| 78 | |
| 79 | Close the tab, and you should be able to observe a request sent to `/test` on |
| 80 | your web server that hosts the page. |
| 81 | |
| 82 | ## Related Links |
| 83 | |
| 84 | - [Chrome Platform Status - Feature: FetchLater API][status] |
| 85 | - [FetchLater Explainer on GitHub](https://github.com/WICG/pending-beacon/blob/main/docs/fetch-later-api.md) |
| 86 | - [FetchLater API Spec (draft)](https://whatpr.org/fetch/1647/9ca4bda...37a66c9.html#dom-global-fetch-later) |
| 87 | - Ask questions about API & Spec via [new issue](https://github.com/WICG/pending-beacon/issues/new) |
| 88 | - [FetchLater API Design Doc in Chromium](https://docs.google.com/document/d/1U8XSnICPY3j-fjzG35UVm6zjwL6LvX6ETU3T8WrzLyQ/edit#heading=h.ms1oipx914vf) |