# Enable Fastly's image optimisation service for URLs ending with
# a common image file extension, or where the browser is indicating
# that the fetch has an image destination
if (req.url.ext ~ "^(jpe?g|gif|png|webp)$" || req.http.Sec-Fetch-Dest == "image") {
  set req.http.x-fastly-imageopto-api = "fastly";
  
  # If a client hint is included, use it to constrain the width
  # of the output image, if no width is already on the querystring
  if (req.http.Viewport-Width && !subfield(req.url.qs, "width", "&")) {
    set req.url = querystring.add(req.url, "width", req.http.Viewport-Width);
  }
}