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';34export default new Router()5 // -------------------------------------6 // Cloud function defined by a connector7 // -------------------------------------89 // 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 route12 .match('/cart', {13 edge_function: './edge-functions/cart.js',14 })1516 // -----------------------------------17 // Cloud function defined by compute()18 // -----------------------------------1920 // defines /session route as a cloud function21 .match('/session', ({compute, addFeatures}) => {22 compute(async (req, res) => {23 // complex logic not suitable for an edge function24 /* ... */2526 res.body = JSON.stringify(/* ... */);27 });2829 // edge function to handle /session route30 addFeatures({ edge_function: './edge-functions/session.js' })31 })3233 // ---------------------------------34 // Cloud function defined by proxy()35 // ---------------------------------3637 // defines /api route as a cloud function38 .match('/api', ({proxy, addFeatures}) => {39 proxy('api', {40 transformResponse: async (res) => {41 // complex logic not suitable for an edge function42 /* ... */4344 res.body = JSON.stringify(/* ... */);45 },46 });4748 // edge function to handle /api route49 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 usingcompute
orproxy
along with thetransformResponse
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';34export default new Router()5 // NextRoutes automatically adds routes for all Next.js pages and their assets6 .use(nextRoutes)78 // '/cart' is a route defined by NextRoutes (eg. ./src/app/cart/page.tsx) but overridden here to be handled by the edge function9 .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;67 // 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 });1718 // Convert the response to text format.19 let responseText = await cloudFunctionsResponse.text();2021 // Manipulate the response to apply personalizations or modifications.22 responseText = responseText.replace(/* ... */);2324 // 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.