A career in the IT industry

Defaulting & Overriding parameters

This page demonstrates the ability to apply default parameters. Inspecting the above image will show that the request only has manipulation parameters and nothing relating to compression, this is dynamically applied in VCL.

You'll also notice that, in supporting browsers, WebP is being delivered due to automatic content negotiation.


  sub vcl_recv {
  #FASTLY recv

    // Return non-image
    if (req.url.path !~ "(?i)\.(?:jpg|png)$" 
      || req.url.path !~ "/images/") {
      return(lookup);
    }

    // Signal to send to IO
    set req.http.X-Fastly-Imageopto-Api = "fastly";

    // Don't alter the request if we're running at 
    // the shield, it has already been rewritten
    if (req.http.Fastly-FF) {
        return (lookup);
    }

    // Declare a local variable for building the new params
    declare local var.qs STRING;
    set var.qs = req.url.qs;
    
    // Override the quality
    if (req.url.qs ~ "^(.*&?)quality=([^&]+)(.*)$") {
      set var.qs = re.group.1 "quality=5" re.group.3;
    } else {
      set var.qs = var.qs + "&quality=5";
    }
    set req.url = req.url.path "?" var.qs;

  }
                

  //facepug.io/images/a4/cat-it.jpg?crop=660:438&width=660

  //facepug.io/images/a4/cat-it.jpg?crop=660:438&width=660&quality=100
                
Share this story