Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix globals, part 11: fetch() #33643

Merged
merged 14 commits into from
Jul 16, 2024
11 changes: 5 additions & 6 deletions files/en-us/_redirects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8383,8 +8383,8 @@
/en-US/docs/Web/API/GlobalEventHandlers/ontransitioncancel /en-US/docs/Web/API/Element/transitioncancel_event
/en-US/docs/Web/API/GlobalEventHandlers/ontransitionend /en-US/docs/Web/API/Element/transitionend_event
/en-US/docs/Web/API/GlobalEventHandlers/onwheel /en-US/docs/Web/API/Element/wheel_event
/en-US/docs/Web/API/GlobalFetch/GlobalFetch.fetch() /en-US/docs/Web/API/fetch
/en-US/docs/Web/API/GlobalFetch/fetch /en-US/docs/Web/API/fetch
/en-US/docs/Web/API/GlobalFetch/GlobalFetch.fetch() /en-US/docs/Web/API/Window/fetch
/en-US/docs/Web/API/GlobalFetch/fetch /en-US/docs/Web/API/Window/fetch
/en-US/docs/Web/API/Guide/Events/Creating_and_triggering_events /en-US/docs/Web/Events/Creating_and_triggering_events
/en-US/docs/Web/API/HID/onconnect /en-US/docs/Web/API/HID/connect_event
/en-US/docs/Web/API/HID/ondisconnect /en-US/docs/Web/API/HID/disconnect_event
Expand Down Expand Up @@ -9728,7 +9728,7 @@
/en-US/docs/Web/API/ServiceWorkerGlobalScope/activate /en-US/docs/Web/API/ServiceWorkerGlobalScope/activate_event
/en-US/docs/Web/API/ServiceWorkerGlobalScope/caches /en-US/docs/Web/API/WorkerGlobalScope/caches
/en-US/docs/Web/API/ServiceWorkerGlobalScope/claim /en-US/docs/Web/API/Clients/claim
/en-US/docs/Web/API/ServiceWorkerGlobalScope/fetch /en-US/docs/Web/API/fetch
/en-US/docs/Web/API/ServiceWorkerGlobalScope/fetch /en-US/docs/Web/API/WorkerGlobalScope/fetch
/en-US/docs/Web/API/ServiceWorkerGlobalScope/install /en-US/docs/Web/API/ServiceWorkerGlobalScope/install_event
/en-US/docs/Web/API/ServiceWorkerGlobalScope/onmessage /en-US/docs/Web/API/ServiceWorkerGlobalScope/message_event
/en-US/docs/Web/API/ServiceWorkerGlobalScope/periodicsync /en-US/docs/Web/API/ServiceWorkerGlobalScope/periodicsync_event
Expand Down Expand Up @@ -10152,7 +10152,6 @@
/en-US/docs/Web/API/Window/content /en-US/docs/Web/API/Window
/en-US/docs/Web/API/Window/convertPointFromNodeToPage /en-US/docs/Web/API/Window/webkitConvertPointFromNodeToPage
/en-US/docs/Web/API/Window/convertPointFromPageToNode /en-US/docs/Web/API/Window/webkitConvertPointFromPageToNode
/en-US/docs/Web/API/Window/fetch /en-US/docs/Web/API/fetch
/en-US/docs/Web/API/Window/minimize /en-US/docs/Web/API/Window
/en-US/docs/Web/API/Window/mozAnimationStartTime /en-US/docs/Web/API/Animation/startTime
/en-US/docs/Web/API/Window/mozPaintCount /en-US/docs/Web/API/Window
Expand Down Expand Up @@ -10257,7 +10256,7 @@
/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout /en-US/docs/Web/API/clearTimeout
/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap /en-US/docs/Web/API/createImageBitmap
/en-US/docs/Web/API/WindowOrWorkerGlobalScope/crossOriginIsolated /en-US/docs/Web/API/Window/crossOriginIsolated
/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch /en-US/docs/Web/API/fetch
/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch /en-US/docs/Web/API/Window/fetch
/en-US/docs/Web/API/WindowOrWorkerGlobalScope/indexedDB /en-US/docs/Web/API/Window/indexedDB
/en-US/docs/Web/API/WindowOrWorkerGlobalScope/isSecureContext /en-US/docs/Web/API/Window/isSecureContext
/en-US/docs/Web/API/WindowOrWorkerGlobalScope/origin /en-US/docs/Web/API/Window/origin
Expand Down Expand Up @@ -10308,7 +10307,6 @@
/en-US/docs/Web/API/WorkerGlobalScope/clearTimeout /en-US/docs/Web/API/clearTimeout
/en-US/docs/Web/API/WorkerGlobalScope/close /en-US/docs/Web/API/DedicatedWorkerGlobalScope/close
/en-US/docs/Web/API/WorkerGlobalScope/console /en-US/docs/Web/API/console
/en-US/docs/Web/API/WorkerGlobalScope/fetch /en-US/docs/Web/API/fetch
/en-US/docs/Web/API/WorkerGlobalScope/onclose /en-US/docs/Web/API/DedicatedWorkerGlobalScope/close
/en-US/docs/Web/API/WorkerGlobalScope/onerror /en-US/docs/Web/API/WorkerGlobalScope/error_event
/en-US/docs/Web/API/WorkerGlobalScope/onlanguagechange /en-US/docs/Web/API/WorkerGlobalScope/languagechange_event
Expand Down Expand Up @@ -10648,6 +10646,7 @@
/en-US/docs/Web/API/event.type /en-US/docs/Web/API/Event/type
/en-US/docs/Web/API/event.view /en-US/docs/Web/API/UIEvent/view
/en-US/docs/Web/API/event.which /en-US/docs/Web/API/UIEvent/which
/en-US/docs/Web/API/fetch /en-US/docs/Web/API/Window/fetch
/en-US/docs/Web/API/form.acceptCharset /en-US/docs/Web/API/HTMLFormElement/acceptCharset
/en-US/docs/Web/API/form.action /en-US/docs/Web/API/HTMLFormElement/action
/en-US/docs/Web/API/form.elements /en-US/docs/Web/API/HTMLFormElement/elements
Expand Down
86 changes: 43 additions & 43 deletions files/en-us/_wikihistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -66867,6 +66867,49 @@
"modified": "2020-10-15T22:11:52.818Z",
"contributors": ["wbamberg", "JeffersonScher", "chrisdavidmills"]
},
"Web/API/Window/fetch": {
"modified": "2020-10-15T21:33:25.494Z",
"contributors": [
"mfuji09",
"Wind1808",
"Sheppy",
"Zearin_Galaurum",
"chrisdavidmills",
"TestPolygon",
"bigjilm",
"tkrotoff",
"old_morfey13",
"skellix",
"FranklinYu",
"sideshowbarker",
"tjcrowder",
"DamienCassou",
"fscholz",
"mfulton26",
"SiliconSoldier",
"satyabhimisetty",
"jakubboucek",
"AngeloKai",
"Yaffle",
"cpplearner",
"Konrud",
"BuonOmo",
"kenrick95",
"kmiyashiro",
"xfq",
"arronei",
"valtlai",
"erikadoyle",
"jpmedley",
"runn1ng",
"ysmood",
"jsx",
"Sebastianz",
"George8211",
"matiishyn",
"kscarfone"
]
},
"Web/API/Window/find": {
"modified": "2020-10-16T15:18:21.626Z",
"contributors": [
Expand Down Expand Up @@ -71556,49 +71599,6 @@
"adria"
]
},
"Web/API/fetch": {
"modified": "2020-10-15T21:33:25.494Z",
"contributors": [
"mfuji09",
"Wind1808",
"Sheppy",
"Zearin_Galaurum",
"chrisdavidmills",
"TestPolygon",
"bigjilm",
"tkrotoff",
"old_morfey13",
"skellix",
"FranklinYu",
"sideshowbarker",
"tjcrowder",
"DamienCassou",
"fscholz",
"mfulton26",
"SiliconSoldier",
"satyabhimisetty",
"jakubboucek",
"AngeloKai",
"Yaffle",
"cpplearner",
"Konrud",
"BuonOmo",
"kenrick95",
"kmiyashiro",
"xfq",
"arronei",
"valtlai",
"erikadoyle",
"jpmedley",
"runn1ng",
"ysmood",
"jsx",
"Sebastianz",
"George8211",
"matiishyn",
"kscarfone"
]
},
"Web/API/queueMicrotask": {
"modified": "2020-10-15T22:21:06.789Z",
"contributors": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
title: fetch() global function
title: "Window: fetch() method"
short-title: fetch()
slug: Web/API/fetch
page-type: web-api-global-function
slug: Web/API/Window/fetch
page-type: web-api-instance-method
browser-compat: api.fetch
---

{{APIRef("Fetch API")}}{{AvailableInWorkers}}
{{APIRef("Fetch API")}}

The global **`fetch()`** method starts the process of fetching a resource from the network, returning a promise that is fulfilled once the response is available.
The **`fetch()`** method of the {{domxref("Window")}} interface starts the process of fetching a resource from the network, returning a promise that is fulfilled once the response is available.

The promise resolves to the {{domxref("Response")}} object representing the response to your request.

Expand Down Expand Up @@ -197,7 +197,8 @@ const myImage = document.querySelector("img");

const myRequest = new Request("flowers.jpg");

fetch(myRequest)
window
.fetch(myRequest)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
Expand All @@ -211,7 +212,7 @@ fetch(myRequest)
```

In our [Fetch Request with init example](https://github.com/mdn/dom-examples/tree/main/fetch/fetch-request-with-init) (see [Fetch Request init live](https://mdn.github.io/dom-examples/fetch/fetch-request-with-init)) we do the same thing except that we pass in an _options_ object when we invoke `fetch()`.
In this case, we can set a {{httpheader("Cache-Control")}} value to indicate what kind of cached responses we're okay with:
In this case, we can set a {{HTTPHeader("Cache-Control")}} value to indicate what kind of cached responses we're okay with:

```js
const myImage = document.querySelector("img");
Expand Down Expand Up @@ -260,6 +261,7 @@ const req = new Request("flowers.jpg", options);

## See also

- {{domxref("WorkerGlobalScope.fetch()")}}
- [Fetch API](/en-US/docs/Web/API/Fetch_API)
- [ServiceWorker API](/en-US/docs/Web/API/Service_Worker_API)
- [HTTP access control (CORS)](/en-US/docs/Web/HTTP/CORS)
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/api/window/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ _This interface inherits methods from the {{domxref("EventTarget")}} interface._
- : Accepts a variety of different image sources, and returns a {{jsxref("Promise")}} which resolves to an {{domxref("ImageBitmap")}}. Optionally the source is cropped to the rectangle of pixels originating at _(sx, sy)_ with width sw, and height sh.
- {{domxref("Window.dump()")}} {{Non-standard_Inline}}
- : Writes a message to the console.
- {{domxref("fetch", "Window.fetch()")}}
- {{domxref("Window.fetch()")}}
- : Starts the process of fetching a resource from the network.
- {{domxref("Window.find()")}} {{Non-standard_Inline}}
- : Searches for a given string in a window.
Expand Down
75 changes: 75 additions & 0 deletions files/en-us/web/api/workerglobalscope/fetch/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: "WorkerGlobalScope: fetch() method"
short-title: fetch()
slug: Web/API/WorkerGlobalScope/fetch
page-type: web-api-instance-method
browser-compat: api.fetch
---

{{APIRef("Fetch API")}}

The **`fetch()`** method of the {{domxref("WorkerGlobalScope")}} interface starts the process of fetching a resource from the network, returning a promise that is fulfilled once the response is available.

The promise resolves to the {{domxref("Response")}} object representing the response to your request.

A `fetch()` promise only rejects when the request fails, for example, because of a badly-formed request URL or a network error.
A `fetch()` promise _does not_ reject if the server responds with HTTP status codes that indicate errors (`404`, `504`, etc.).
Instead, a `then()` handler must check the {{domxref("Response.ok")}} and/or {{domxref("Response.status")}} properties.

The `fetch()` method is controlled by the `connect-src` directive of [Content Security Policy](/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) rather than the directive of the resources it's retrieving.

> **Note:** The `fetch()` method's parameters are identical to those of the {{domxref("Request.Request","Request()")}} constructor.

## Syntax

```js-nolint
fetch(resource)
fetch(resource, options)
```

### Parameters

- `resource`

- : This defines the resource that you wish to fetch. This can either be:

- A string or any other object with a {{Glossary("stringifier")}} — including a {{domxref("URL")}} object — that provides the URL of the resource you want to fetch. The URL may be relative to the base URL, which is the document's {{domxref("Node.baseURI", "baseURI")}} in a window context, or {{domxref("WorkerGlobalScope.location")}} in a worker context.
- A {{domxref("Request")}} object.

- `options` {{optional_inline}}

- : A {{domxref("RequestInit")}} object containing any custom settings that you want to apply to the request.

### Return value

A {{jsxref("Promise")}} that resolves to a {{domxref("Response")}} object.

### Exceptions

- `AbortError` {{domxref("DOMException")}}
- : The request was aborted due to a call to the {{domxref("AbortController")}} {{domxref("AbortController.abort", "abort()")}} method.
- `NotAllowedError` {{domxref("DOMException")}}
- : Thrown if use of the [Topics API](/en-US/docs/Web/API/Topics_API) is specifically disallowed by a {{httpheader('Permissions-Policy/browsing-topics','browsing-topics')}} [Permissions Policy](/en-US/docs/Web/HTTP/Permissions_Policy), and a `fetch()` request was made with `browsingTopics: true`.
- {{jsxref("TypeError")}}
- : An error when the fetch operation could not be performed.
See {{domxref("Window.fetch()")}} exceptions for a list of reasons why this error can occur.

## Examples

See {{domxref("Window.fetch", "fetch()")}} for examples.

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{domxref("Window.fetch()")}}
- [Fetch API](/en-US/docs/Web/API/Fetch_API)
- [ServiceWorker API](/en-US/docs/Web/API/Service_Worker_API)
- [HTTP access control (CORS)](/en-US/docs/Web/HTTP/CORS)
- [HTTP](/en-US/docs/Web/HTTP)
2 changes: 1 addition & 1 deletion files/en-us/web/api/workerglobalscope/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ _This interface inherits methods from the {{domxref("EventTarget")}} interface._
- : Accepts a variety of different image sources, and returns a {{jsxref("Promise")}} which resolves to an {{domxref("ImageBitmap")}}. Optionally the source is cropped to the rectangle of pixels originating at _(sx, sy)_ with width sw, and height sh.
- {{domxref("WorkerGlobalScope.dump()")}} {{deprecated_inline}} {{non-standard_inline}}
- : Allows you to write a message to stdout — i.e. in your terminal. This is the same as Firefox's {{domxref("window.dump")}}, but for workers.
- {{domxref("fetch()", "WorkerGlobalScope.fetch()")}}
- {{domxref("WorkerGlobalScope.fetch()")}}
- : Starts the process of fetching a resource from the network.
- {{domxref("WorkerGlobalScope.importScripts()")}}
- : Imports one or more scripts into the worker's scope. You can specify as many as you'd like, separated by commas. For example: `importScripts('foo.js', 'bar.js');`.
Expand Down
2 changes: 1 addition & 1 deletion files/jsondata/GroupData.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@
"overview": ["Fetch API"],
"guides": ["/docs/Web/API/Fetch_API/Using_Fetch"],
"interfaces": ["Headers", "Request", "RequestInit", "Response"],
"methods": ["fetch()"],
"methods": ["Window.fetch()", "WorkerGlobalScope.fetch()"],
"properties": [],
"events": []
},
Expand Down