Fetch
API is designed based on the standard Web API Fetch. You can add fetch
to the edge function runtime to initiate an async request to fetch remote.function fetch(request: string | Request, requestInit?: RequestInit): Promise<Response>
Parameter name | Type | Required | Description |
request | string | Request | Yes | Requested resource. |
requestInit | No |
fetch
, you can pass in specific parameters for finer configuration of EdgeOne node caching, fetching from the origin, image processing and redirection.fetch(www.example.com)
is executed in the edge function. The request is directed to the cache on the EdgeOne node. If no cache is hit, the request is redirected to the origin.request.url
specified in fetch(request)
is the same as the HOST of client request URL.request.headers.host
specified in fetch(request)
is the same as the HOST of client request URL.fetch(event.request)
to obtain the cache from EdgeOne nodes and pull resources from the origin if no cache is hit.addEventListener('fetch', (event) => {//Use `fetch(event.request)` to obtain the cache from EdgeOne nodes and pull resources from the origin if no cache is hit.const response = fetch(event.request);event.respondWith(response);});
fetch(url)
to obtain the cache from EdgeOne nodes and pull resources from the origin if no cache is hit.addEventListener('fetch', (event) => {event.respondWith(handleEvent(event));});async function handleEvent(event) {const { request } = event;const urlInfo = new URL(request.url);// origin-pull URL rewriteconst url = `${urlInfo.origin}/h5/${urlInfo.pathname}`;// fetch(url) collected EdgeOne CDN Cache and origin-pull.const response = await fetch(url);return response;}
requestInit.eo.image
to scale images or convert the image format. For details, see ImageProperties.fetch(request, requestInit)
to process images, the requirements for using fetch
to obtain cache and pull from the origin must be met.fetch
supports 3xx
redirect status codes. You can use the second parameter requestInit.redirect
to specify the redirect status code. For more information, see RequestInit.Status code | Redirect Rule |
301 and 302 | Replaces the POST method with the GET method. |
303 | Replaces all request methods except for HEAD and GET with the GET method. |
307 and 308 | Retains the original request method. |
Location
response header. If Location
does not exist, no redirect action is taken.Location
can be an absolute URL or a relative URL. For more information, see RFC-3986: URI Reference.fetch
to initiate a request in an edge function, take note of the following limits:fetch
can be initiated for a maximum of 64 times. If the limit is exceeded, the exceeding requests fail and exceptions are returned.fetch
can be initiated at a maximum concurrency of 8. If the limit is exceeded, the exceeding requests are postponed until a running fetch
is resolved.fetch
requests.
Was this page helpful?