Edgio

Fetching from Cloud Functions

Fetching from a cloud function is similar to fetching from an origin server. The key difference is that you must specify the edgio_serverless origin in the request. This instructs the request to the cloud function origin where it is then handled by your JavaScript backend.
The following sample code shows different ways a cloud function might be defined:
JavaScript./routes.js
1import {Router} from '@edgio/core/router';
2import {nextRoutes} from '@edgio/next';
3
4export default new Router()
5 // -------------------------------------
6 // Cloud function defined by a connector
7 // -------------------------------------
8
9 // defines /cart route based on Next.js App/Pages router (eg. ./src/app/cart/page.tsx)
10 .use(nextRoutes)
11 // edge function to handle /cart route
12 .match('/cart', {
13 edge_function: './edge-functions/cart.js',
14 })
15
16 // -----------------------------------
17 // Cloud function defined by compute()
18 // -----------------------------------
19
20 // defines /session route as a cloud function
21 .match('/session', ({compute, addFeatures}) => {
22 compute(async (req, res) => {
23 // complex logic not suitable for an edge function
24 /* ... */
25
26 res.body = JSON.stringify(/* ... */);
27 });
28
29 // edge function to handle /session route
30 addFeatures({ edge_function: './edge-functions/session.js' })
31 })
32
33 // ---------------------------------
34 // Cloud function defined by proxy()
35 // ---------------------------------
36
37 // defines /api route as a cloud function
38 .match('/api', ({proxy, addFeatures}) => {
39 proxy('api', {
40 transformResponse: async (res) => {
41 // complex logic not suitable for an edge function
42 /* ... */
43
44 res.body = JSON.stringify(/* ... */);
45 },
46 });
47
48 // edge function to handle /api route
49 addFeatures({ edge_function: './edge-functions/api.js' })
50 })
For example, when using a framework compatible with Sites like Next.js, you can forward the incoming request to the Next.js server. This allows you to process and modify the response that Next.js provides at the edge before sending it back to the client, enabling personalization and other adjustments.
To fetch from a cloud function, you must meet the following requirements:
  • Edgio version 7.4.1 or later.
  • A route that is defined as a cloud function. This can be a route via a connector such as NextRoutes or by using compute or proxy along with the transformResponse option.
  • A route that uses an edge function. This must match the path as the cloud function and be defined after the cloud function route (see sample code above).
  • The origin edgio_serverless must be specified in the request (see System-Defined Origins).
  • Forwarding of the original request parameters including the method, headers, and body.
The following sample code demonstrates how to fetch and manipulate cloud function response within an edge function:
JavaScript./routes.js
1import {Router} from '@edgio/core/router';
2import {nextRoutes} from '@edgio/next';
3
4export default new Router()
5 // NextRoutes automatically adds routes for all Next.js pages and their assets
6 .use(nextRoutes)
7
8 // '/cart' is a route defined by NextRoutes (eg. ./src/app/cart/page.tsx) but overridden here to be handled by the edge function
9 .match('/cart', {
10 edge_function: './edge-functions/cart.js',
11 });
JavaScript./edge-functions/cart.js
1export async function handleHttpRequest(request) {
2 // Check the request method and get the request body as an ArrayBuffer if it's not a GET or HEAD request.
3 const requestBody = !['GET', 'HEAD'].includes(request.method)
4 ? await request.arrayBuffer()
5 : undefined;
6
7 // Perform a fetch request to the original request URL with the same method, headers, and body.
8 // Specify 'edgio_serverless' as the origin to fetch the original Cloud Functions response.
9 const cloudFunctionsResponse = await fetch(request.url, {
10 edgio: {
11 origin: 'edgio_serverless',
12 },
13 method: request.method,
14 headers: request.headers,
15 body: requestBody,
16 });
17
18 // Convert the response to text format.
19 let responseText = await cloudFunctionsResponse.text();
20
21 // Manipulate the response to apply personalizations or modifications.
22 responseText = responseText.replace(/* ... */);
23
24 // Return a new response with the modified text and original response status, status text, and headers.
25 return new Response(responseText, {
26 status: cloudFunctionsResponse.status,
27 statusText: cloudFunctionsResponse.statusText,
28 headers: cloudFunctionsResponse.headers,
29 });
30}

Limitations

Fetching from a Cloud Function is considered an origin subrequest and therefore has the same limitations. See Fetch Limitations for more information.