Hi there, has anyone had any luck with using a Clo...
# support
a
Hi there, has anyone had any luck with using a Cloudflare worker to reverse proxy the data plane url for the v3 SDK? I have created a worker with the below code (KV is the data plane url) but can't see any events come through when testing. export default { async fetch(request, env, ctx) { // Check the request URL path for the SDK route const url = new URL(request.url); // Define the path where you want to serve the SDK const SDK_PATH = '/sdk'; // Customize this path as needed if (url.pathname === SDK_PATH) { // If the request matches the SDK route, fetch the SDK const SDK_URL = 'https://cdn.rudderlabs.com/beta/3.0.0-beta'; const response = await fetch(SDK_URL); // Return the response received from the SDK URL return response; } // If the request does not match the SDK route, handle other routes async function fetchOriginMappings(hostname) { return await env.KV.get(hostname); } async function handleRequest(request) { const DATA_PLANE_URL = await fetchOriginMappings(url.hostname); // Check if incoming hostname is a key in the originMappings object if (DATA_PLANE_URL) { url.hostname = DATA_PLANE_URL; // Forward the request to the third-party origin const response = await fetch(url.toString(), request); // Check the response status and handle accordingly if (response.status === 200) { // If it is a successful response, return it return response; } else { // Handle other response statuses as needed return new Response("Proxy request failed", { status: response.status, statusText: response.statusText, }); } } // Otherwise, return a 401 Unauthorized response return new Response("Unauthorized", { status: 401 }); } return handleRequest(request); }, };
m
Hey There! 👋 Your message has been received by the RudderStack team. Our standard customer support hours are 9-6 PM EST, but we will forward this request to your Technical Account Manager, and they will get back to you shortly. Please use the thread for any additional comments.
a
Solved it for the V1 SDK. For everyone else's reference the below code worked. I have the data plane url saved as a CloudFlare KV. I would love to hear if anyone has managed to get this working with the V3 SDK.
// Define your RudderStack data plane URL const RUDDERSTACK_SDK_V1_URL = 'https://cdn.rudderlabs.com/v1/rudder-analytics.min.js'; async function getDataPlaneUrl(hostname) { return await KV.get(hostname); } async function handleRequest(request) { const url = new URL(request.url); const RUDDERSTACK_DATA_PLANE_URL = await getDataPlaneUrl(url.hostname); if (RUDDERSTACK_DATA_PLANE_URL) { let targetURL; // This will hold the URL we want to fetch data from if (url.pathname === '/v1sdk') { // If the request is for the v1 SDK, set the target URL to RudderStack's SDK URL targetURL = RUDDERSTACK_SDK_V1_URL; } else { // Otherwise, forward the request to RudderStack's data plane URL targetURL = `https://${RUDDERSTACK_DATA_PLANE_URL}${url.pathname}`; } let modifiedRequest = new Request(targetURL, request); // Fetch the response from the target server let response = await fetch(modifiedRequest); // Clone the response to modify headers for CORS let modifiedResponse = new Response(response.body, response); modifiedResponse.headers.set("Access-Control-Allow-Origin", "*"); return modifiedResponse; } else { return new Response("Unauthorized", { status: 401, statusText: "Unauthorized" }); } } addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); });