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 | Specifies the request resource to be obtained. |
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.fetch, the following conditions must be met:request.url specified by fetch(request) is the same as the HOST in the client request URL.request.headers.host specified in fetch(request) is the same as the HOST of client request URL.fetch will perform origin-pull over the public network.fetch(event.request) to obtain the cache from EdgeOne nodes and pull resources from the origin if no cache is hit.addEventListener('fetch', (event) => {// fetch(event.request) obtains the EdgeOne CDN cache and origin-pull.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) obtains the 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), the conditions for fetch to obtain the EdgeOne node cache and origin-pull must also be met.fetch supports configuring the request timeout using requestInit.eo.timeoutSetting.The default timeout for establishing a connection is 3 seconds, the default timeout for reading the response is 15 seconds, and the default timeout for sending the request is 300 seconds. The maximum value that can be set is 300 seconds. If the connection establishment times out, a net_exception_connect_timeout exception is thrown; if reading the response or sending the request times out, a net_exception_timeout exception is thrown.fetch(url, {eo: {timeoutSetting: {connectTimeout: 60000, // Timeout for establishing a connection, in milliseconds. Default value: 3000.readTimeout: 60000, // Timeout for reading the response, in milliseconds. Default value: 15000.writeTimeout: 60000, // Timeout for sending the request, in milliseconds. Default value: 300000.}}})
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.fetch is to automatically follow redirects, which means the default value of the requestInit.redirect property in the second parameter is follow. This may cause issues in the following scenarios:Set-Cookie header in a redirect response (for example, it returns 302 and sets a session Cookie after a successful login). Automatically following redirects prevents the Cookie from being correctly delivered to the client./ is redirected to /subdir/index.html). If resources such as CSS/JS on the page use relative paths, automatically following redirects causes the client to load resources based on the original path, which may result in incorrect paths.redirect parameter to manual. Edge Functions will directly return the redirect response to the client, and the client will perform the redirect.const response = await fetch(url, { redirect: 'manual' });
Status Code | Redirection Rules |
301,302 | The POST method is converted to the GET method. |
303 | All methods except HEAD/GET are converted to the GET method. |
307,308 | Preserve the original 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 is used in edge functions, the default timeout for establishing a connection is 3 seconds, the default timeout for reading a response is 15 seconds, and the default timeout for sending a request is 300 seconds, with a maximum configurable value of 300 seconds.fetch requests.Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback