Skip to the code

Negotiate an image format

The tea shop's image server keeps every photo as AVIF, WebP, and JPEG, and sends each browser the best one it can show. Write negotiateImageFormat(accept). Given a request's Accept header, or undefined when the request has none, return "image/avif", "image/webp", or "image/jpeg", or null when the browser accepts none of them.

Read the header the way RFC 9110 says. It's a comma-separated list of media ranges: a type like image/webp, image/* for any image, or */* for anything, each with optional parameters after semicolons. The q parameter (q or Q) is a weight from 0 to 1, and 1 when it isn't there; q=0 means not acceptable. Types are compared in any case, and spaces around commas and semicolons don't matter.

A format's weight comes from the most specific range that covers it: image/webp beats image/*, which beats */*. So image/*;q=0.8, image/webp;q=0 rules WebP out and gives every other image 0.8. Pick the format with the highest weight above 0. When formats tie, use the server's own order of preference: AVIF, then WebP, then JPEG, which is usually smallest file first. A request without an Accept header accepts anything, so it gets AVIF.

When no format has a weight above 0, return null. RFC 9110 lets the server answer that with 406 Not Acceptable, or ignore the header and send what it has.

// The Accept header Chrome sends for an image
negotiateImageFormat("image/jxl,image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8"); // "image/avif"
negotiateImageFormat("image/webp,*/*;q=0.8"); // "image/webp": AVIF only gets 0.8
negotiateImageFormat("image/avif;q=0, image/*"); // "image/webp"

What the tests check

  1. the Accept header Chrome sends for an image gets AVIF
    expect(negotiateImageFormat("image/jxl,image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8")).toBe("image/avif");
    
  2. takes the format with the highest weight
    expect(negotiateImageFormat("image/webp,image/avif;q=0.5,*/*;q=0.1")).toBe("image/webp");
    expect(negotiateImageFormat("image/jpeg, image/webp;q=0.9")).toBe("image/jpeg");
    expect(negotiateImageFormat("image/avif;q=0.6, image/webp;q=0.7, image/jpeg;q=0.65")).toBe("image/webp");
    expect(negotiateImageFormat("image/webp,*/*;q=0.8")).toBe("image/webp");
    
  3. ties go to AVIF, then WebP, then JPEG
    expect(negotiateImageFormat("image/jpeg, image/webp, image/avif")).toBe("image/avif");
    expect(negotiateImageFormat("image/jpeg,image/webp")).toBe("image/webp");
    expect(negotiateImageFormat("image/jpeg;q=0.5, image/webp;q=0.5")).toBe("image/webp");
    expect(negotiateImageFormat("image/*")).toBe("image/avif");
    expect(negotiateImageFormat("*/*")).toBe("image/avif");
    
  4. q=0 rules a format out, even where a wildcard would take it
    expect(negotiateImageFormat("image/avif;q=0, image/*")).toBe("image/webp");
    expect(negotiateImageFormat("image/*;q=0.8, image/webp;q=0, image/avif;q=0")).toBe("image/jpeg");
    expect(negotiateImageFormat("image/webp;q=0")).toBe(null);
    
  5. the most specific range sets the weight, higher or lower than a wildcard's
    expect(negotiateImageFormat("image/*;q=0.5, image/webp")).toBe("image/webp");
    expect(negotiateImageFormat("image/*, image/avif;q=0.3")).toBe("image/webp");
    expect(negotiateImageFormat("*/*;q=0.9, image/*;q=0.2, image/jpeg")).toBe("image/jpeg");
    expect(negotiateImageFormat("image/jpeg;q=0.4, */*;q=0.5")).toBe("image/avif");
    
  6. a request without an Accept header accepts anything
    expect(negotiateImageFormat(undefined)).toBe("image/avif");
    expect(negotiateImageFormat()).toBe("image/avif");
    
  7. null when no format is acceptable
    expect(negotiateImageFormat("text/html,application/xhtml+xml")).toBe(null);
    expect(negotiateImageFormat("image/png,image/gif")).toBe(null);
    expect(negotiateImageFormat("*/*;q=0")).toBe(null);
    
  8. reads types and q in any case, with spaces around commas and semicolons
    expect(negotiateImageFormat("IMAGE/WEBP ; Q=0.7 , image/JPEG;q=0.6")).toBe("image/webp");
    expect(negotiateImageFormat(" image/avif ;q=0.9,image/webp")).toBe("image/webp");
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).