Issue with Appending Query Strings to Origin Requests in Edgio CDN as Code

Hello,

I’m encountering an issue with appending query strings to the origin requests using Edgio CDN as Code. I want to modify the request URL sent to the origin by appending a query string without exposing it to the client. However, the query string isn’t being appended as expected, and I’m not seeing any logs that I’ve added for debugging purposes.

Setup Details:

  1. Edgio Version: 7.12.11
  2. Environment: Local development (edgio dev)
  3. Router Configuration File: Below is the relevant part of my routes.js or router.js file:
import { Router, edgioRoutes } from '@edgio/core'

export default new Router()
  .match('/', ({ proxy }) => {
    proxy('origin', {
      path: ({ request }) => {
        const originalPath = request.url
        const newQueryString = 'sp=r&st=2024-08-02T08:28:02Z&se=2025-02-08T08:28:02Z&spr=https&sv=2022-11-02&sr=c&sig=abcd'
        
        // Log the original path
        console.log('Original Path:', originalPath)
        
        // Construct the new path with the query string
        const newPath = `${originalPath}?${newQueryString}`
        
        // Log the new path
        console.log('New Path with Query String:', newPath)
        
        return newPath
      }
    })
  })

  .use(edgioRoutes)

Issue Details:

  1. No Logs: I have added console.log statements for debugging, but I’m not seeing any output in the terminal or console.
  2. Query String Not Appended: The query string does not appear to be appended to the URL sent to the origin.

What I’ve Tried:

  • Running the setup locally using edgio dev.
  • Inspecting the network requests using the browser’s developer tools.

Questions:

  1. Are there specific configurations or permissions required to see the logs in the terminal or console?
  2. Is there a different method to append query strings to the origin request URL?
  3. How can I effectively debug this issue and ensure the query string is appended correctly?

Any help or guidance would be greatly appreciated. Thank you!

Hello,

Based on the Edgio documentation provided, here are some insights into the issues you’re facing:

  1. No Logs: To view server logs in the Edgio Console, you need to look at the messages logged by your application using console.log, console.warn, etc. If you have enabled Deep Request Inspection, you can also see the headers and body of every request and response served by your application through the Edgio cloud. Make sure you are checking the server logs in the Edgio Console to see the output of your console.log statements.

  2. Query String Not Appended: The method you are using to append the query string to the origin request URL seems correct according to the Edgio documentation. However, if the query string is not being appended as expected, you might want to ensure that the path function is being used correctly within the proxy method. The path function should return the modified path, which you are already doing in your code.

  3. Debugging: To debug issues related to routing to your origin, you can temporarily move the proxy from the edge to the Edgio cloud by using transformRequest and transformResponse within the proxy method. This will allow you to log request and response properties that you want to troubleshoot. Once deployed, you can observe the output in your server logs. Remember to proxy traffic from the edge whenever possible for better performance and to avoid Edgio cloud surcharges. The solution to move proxying to the Edgio cloud should only be used as a temporary measure while addressing issues.

For effective debugging, you can also use the curl command to confirm the behavior of the origin server by bypassing DNS resolution or specifying cookies and user agents.

If you continue to face issues, you might want to check the Edgio CLI troubleshooting section for common errors and their resolutions. For example, ensuring that you have the latest version of the Edgio CLI installed can resolve certain issues.

References:


Edgio Answers may provide inaccurate information and should be verified. Consult our official documentation for more information.

@Yash ,

You should certainly be getting a build error with that code. The path property does not accept a request argument. That code is also executed at build time, not runtime. It will not have reference to any current request URL.

Since it looks like you are wanting to conditionally add query params to your request, you can use the transformRequest handler which is executed in the cloud runtime and will have access to the current request. You could do something like:

.match('/', ({ proxy }) => {
  proxy('origin', {
    transformRequest: (request) => {
      const url = new URL(
        request.url, // pathname and query
        `${request.protocol}://${request.headers.host}`
      );

      console.log('Original URL:', url.toString());

      const newQueryString =
        'sp=r&st=2024-08-02T08:28:02Z&se=2025-02-08T08:28:02Z&spr=https&sv=2022-11-02&sr=c&sig=abcd';
      newQueryString.split('&').forEach((param) => {
        const [key, value] = param.split('=');
        url.searchParams.set(key, value);
      });

      console.log('Updated URL:', url.toString());

      request.url = url.pathname + url.search;
    },
  });
})

This modifies the url property of the original request before it is forwarded to your origin. Because that function is now being invoked as the request comes in, you will see the console log output both locally and when deployed if you have Deep Request Inspection enabled.