Skip to content

Feature Request: first request WebP, second request: AVIF #79

@Aqours

Description

@Aqours

Background

Lamada takes long time to transform image to avif format.

Image

Solution: Stale-While-Revalidate

stale-while-revalidate is a caching setting. It’s a parameter in the Cache-Control header, and it tells CDNs how long they can keep serving the image after it expires:

Cache-Control: max-age=3600, stale-while-revalidate=60
               ↑ how long a file can be cached for
                             ↑ how long a CDN can keep serving
                               the file after max-age expires

Here’s how we used it to make sure AVIF never makes image responses slow:

  1. First Request: WebP
    On the first request, we serve the image as WebP, not as AVIF.

We also set the Cache-Control header to max-age=0, stale-while-revalidate=31536000

  1. Immediate Expiry
    Because max-age is set to 0, the WebP image expires immediately. This prompts the CDN to forward the second request to us.

  2. Second Request: AVIF
    When the second request arrives, we serve the image as AVIF.

Responding to the second request can take several seconds because AVIF conversion is slow. But thanks to stale-while-revalidate, our CDN (CloudFront) keeps serving the WebP image until the conversion is complete.

We recognize the second request from the first by the If-None-Match header. Only the second request has it.

When the AVIF image is ready, we return it with Cache-Control: max-age=31536000. This allows the CDN to cache and serve it for a long time.

# See More

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions