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 | |
Ming-Ying Chung | 251a52dc | 2024-01-19 07:35:30 | [diff] [blame] | 9 | Starting from [version 121][status], Chrome experimentally supports the |
Ming-Ying Chung | 141600c | 2023-11-14 03:12:41 | [diff] [blame] | 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)) |
Ming-Ying Chung | 251a52dc | 2024-01-19 07:35:30 | [diff] [blame] | 45 | - A fetchLater request is not observable in Chrome DevTools after its initiating |
| 46 | document is closed, which is due to the current |
| 47 | [DevTools limitation](https://chromestatus.com/feature/4654499737632768?gate=4947446974644224); |
| 48 | if the document is still alive, the request should be visible. |
Ming-Ying Chung | 141600c | 2023-11-14 03:12:41 | [diff] [blame] | 49 | |
| 50 | The following features work differently than the one described in explainer and |
| 51 | spec: |
| 52 | |
| 53 | - To address the privacy requirement (see |
Ming-Ying Chung | 251a52dc | 2024-01-19 07:35:30 | [diff] [blame] | 54 | [pending-beacon/issues/30](https://github.com/WICG/pending-beacon/issues/30#issuecomment-1888554622)), |
| 55 | any pending fetchLater requests on a document are **all flushed out** if the |
| 56 | document enters BFCache, no matter BackgroundSync permission is on or not. |
| 57 | - The maximum time a fetchLater request can be pending is bound by Chrome's |
| 58 | back/forward cache TTL, which is currently **10 minutes** after page goes into |
| 59 | the cache. Note that due to the above forced-flushing behavior, in reality |
| 60 | there should be no request pending after a page being cached. |
Ming-Ying Chung | 141600c | 2023-11-14 03:12:41 | [diff] [blame] | 61 | |
| 62 | ## Activation |
| 63 | |
| 64 | The API can be enabled by a command line flag. |
| 65 | |
| 66 | ### Using command line flag |
| 67 | |
| 68 | Passing `--enable-features=FetchLaterAPI --enable-blink-features=FetchLaterAPI` |
| 69 | command line flag to Chrome enables FetchLater API support. |
| 70 | |
| 71 | ### Verifying the API is working |
| 72 | |
| 73 | Added the following line to an HTTPS web page, and load the page into a Chrome |
| 74 | tab. |
| 75 | |
| 76 | ```html |
| 77 | <script> |
| 78 | fetchLater('/test'); |
| 79 | </script> |
| 80 | ``` |
| 81 | |
| 82 | Close the tab, and you should be able to observe a request sent to `/test` on |
| 83 | your web server that hosts the page. |
| 84 | |
| 85 | ## Related Links |
| 86 | |
| 87 | - [Chrome Platform Status - Feature: FetchLater API][status] |
| 88 | - [FetchLater Explainer on GitHub](https://github.com/WICG/pending-beacon/blob/main/docs/fetch-later-api.md) |
| 89 | - [FetchLater API Spec (draft)](https://whatpr.org/fetch/1647/9ca4bda...37a66c9.html#dom-global-fetch-later) |
| 90 | - Ask questions about API & Spec via [new issue](https://github.com/WICG/pending-beacon/issues/new) |
| 91 | - [FetchLater API Design Doc in Chromium](https://docs.google.com/document/d/1U8XSnICPY3j-fjzG35UVm6zjwL6LvX6ETU3T8WrzLyQ/edit#heading=h.ms1oipx914vf) |