Skip to content

fix(PPT-764): Prefer AVIF over WebP in format auto-selection#5

Open
adityaverm-a wants to merge 1 commit into
mainfrom
ppt-764
Open

fix(PPT-764): Prefer AVIF over WebP in format auto-selection#5
adityaverm-a wants to merge 1 commit into
mainfrom
ppt-764

Conversation

@adityaverm-a
Copy link
Copy Markdown
Collaborator

Summary

  • Reorder FORMAT_PRIORITY from webp > avif to avif > webp in the container auto-optimizer and the CloudFront dit-header-normalization function
  • When a client sends Accept: image/avif,image/webp,..., DIT now selects AVIF instead of always falling back to WebP
  • Sharp AVIF encoding was already supported; the bug was priority ordering in format selection at both the edge and container layers

Root cause

FORMAT_PRIORITY ranked WebP above AVIF in two places:

  • source/container/.../auto-optimizer.ts — container format auto-selection
  • source/constructs/lib/v8/functions/dit-header-normalization.js — CF Function collapses Accept → single dit-accept header

If only the container changed, production would still serve WebP because the CF Function strips image/avif before the request reaches the container.

Test plan

  • Unit tests: auto-optimizer.test.ts — avif preferred when both avif+webp accepted
  • Unit tests: dit-function.test.ts — Accept image/avif,image/webp,image/pngdit-accept: image/avif
  • Local dev: explicit ?format=avif&quality=80 → valid AVIF output (547 B from test.png)
  • Local dev: auto-format with dit-accept: image/avif,image/webp → AVIF (547 B), not WebP (370 B)
  • Post-deploy: verify CF Function + container both deployed together
  • Post-deploy: curl -H "Accept: image/avif,image/webp,image/*" <dit-url> returns AVIF

Deploy note

Both the CloudFront Function and the container image must ship together. Deploying only one layer leaves AVIF broken.

Known follow-up

Sharp reports AVIF output as Content-Type: image/heif; Chrome may download instead of inline-render when hitting the URL directly. File bytes are valid AVIF. Consider mapping heifavif in response headers as a separate fix.

Made with Cursor

Reorder FORMAT_PRIORITY to avif > webp in both the container auto-optimizer
and the CloudFront dit-header-normalization function so clients advertising
image/avif receive AVIF instead of always falling back to WebP.

Co-authored-by: Cursor <cursoragent@cursor.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes automatic image format selection so that AVIF is preferred over WebP when a client supports both, keeping CloudFront header normalization and the container auto-optimizer in sync.

Changes:

  • Reordered FORMAT_PRIORITY to prefer avif over webp in both the container auto-optimizer and the CloudFront dit-header-normalization function.
  • Updated/added unit tests to assert AVIF is chosen when both AVIF and WebP are accepted.
  • Updated the infrastructure snapshot to reflect the CloudFront Function code change.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
source/container/src/services/transformation-resolver/auto-optimization/auto-optimizer.ts Changes container-side FORMAT_PRIORITY so auto format selection picks AVIF before WebP.
source/container/src/services/transformation-resolver/auto-optimization/auto-optimizer.test.ts Updates/adds unit tests to validate AVIF preference in auto-selection.
source/constructs/lib/v8/functions/dit-header-normalization.js Changes edge-side FORMAT_PRIORITY so Accept normalization selects AVIF first.
source/constructs/lib/v8/test/unit/dit-function.test.ts Updates unit test expectation for AVIF vs WebP selection from Accept.
source/constructs/lib/v8/test/snapshot/snapshots/image-processing-stack.test.ts.snap Updates snapshot to match the new CloudFront Function priority ordering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +8 to 9
const FORMAT_PRIORITY = ['avif', 'webp', 'jpeg', 'png', 'heif', 'tiff', 'raw', 'gif'];
// TODO, DISCUSS WITH TEAM FOR OPTIMAL FORMAT PRIORITIY LIST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants