Stape/Documentation

How to use same origin through Netlify

Updated Mar 28, 2025

This instruction uses /metrics as an example of the same origin path. You can use any of your own.

You should also use the URL of your sGTM container instead of https://sgtm.example.com.

Step 1. Create an Edge Function file

e.g. netlify/edge-functions/metrics.js

// netlify/functions/metrics-proxy.js const TARGET_URL = "https://sgtm.example.com"; exports.handler = async (event, context) => { // Netlify often includes the client’s IP in x-nf-client-connection-ip const clientIp = event.headers["x-nf-client-connection-ip"] || "unknown"; // Build new headers object const newHeaders = { ...event.headers, "X-Forwarded-For": clientIp, "X-From-Cdn": "cf-stape", "CF-Connecting-Ip": clientIp, "Host": "sgtm.example.com", }; // Proxy the request to your target const response = await fetch(TARGET_URL, { method: event.httpMethod, headers: newHeaders, body: event.body, // pass the original request body }); // Build the response for Netlify const responseBody = await response.text(); return { statusCode: response.status, headers: Object.fromEntries(response.headers.entries()), body: responseBody, }; };

Step 2. Test a deployment

Deploy these changes to your Netlify site.

You can verify the proxy is working by opening your proxy path in the browser. Go to https://sgtm.example.com/metrics. If you see error 400, it means everything is working correctly. You can also run a preview of the server container on your /metrics path.

After following these steps, add your /path for the same origin to the Custom Loader's settings. Check the article on Same Origin Path to do this.

Can’t find what you are looking for?