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 is to automatically follow redirects, meaning the second parameter requestInit.redirect defaults to "follow". This can cause issues in the following scenarios:Set-Cookie header in a redirect response (such as returning a 302 and setting a session cookie after successful login), automatically following redirects may prevent cookies from being correctly passed to the client;/ redirecting to /subdir/index.html), if page resources like CSS/JS use relative paths, automatically following redirects may cause the client to load resources based on the original path, potentially resulting in path errors;redirect parameter to "manual". The edge function will return the redirect response directly to the client, allowing the client to execute the redirect.fetch(request, requestInit) to process images, the requirements for using fetch to obtain cache and pull from the origin must be met.Fetch supports configuring the request timeout using requestInit.eo.timeoutSetting, with a default timeout of 15 seconds and a maximum of 300 seconds.fetch(url, {eo: {timeoutSetting: {connectTimeout: 60000, // Connection timeout, unit: millisecondsreadTimeout: 60000, // Response read timeout, unit: millisecondswriteTimeout: 60000, // Request send timeout, unit: milliseconds}}})
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 in Edge functions is 15 seconds, with a maximum setting of 300 seconds.fetch requests.Feedback