Skip to content
⚠️ This article was written in 2020. Some content may be outdated.

Frontend Image Optimization: WebP and AVIF

The topic of frontend image optimization with WebP and AVIF has been discussed many times in the community, but as versions iterate, many conclusions need updating. This article revisits it based on the latest versions.

Getting Started ​

The key is to understand the core logic:

javascript
const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.entryType === 'largest-contentful-paint') {
      reportMetric('LCP', entry.startTime)
    }
    if (entry.entryType === 'first-input') {
      reportMetric('FID', entry.processingStart - entry.startTime)
    }
  }
})
observer.observe({ entryTypes: ['largest-contentful-paint', 'first-input'] })

Performance optimization needs to fit the specific scenario; not every case calls for over-optimization.

Source Code Analysis ​

We can improve things in the following ways:

javascript
const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.entryType === 'largest-contentful-paint') {
      reportMetric('LCP', entry.startTime)
    }
    if (entry.entryType === 'first-input') {
      reportMetric('FID', entry.processingStart - entry.startTime)
    }
  }
})
observer.observe({ entryTypes: ['largest-contentful-paint', 'first-input'] })

This approach has been running stably in production for over half a year and has been proven in practice.

Real-World Applications ​

Let's first look at the basic implementation:

javascript
const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.entryType === 'largest-contentful-paint') {
      reportMetric('LCP', entry.startTime)
    }
    if (entry.entryType === 'first-input') {
      reportMetric('FID', entry.processingStart - entry.startTime)
    }
  }
})
observer.observe({ entryTypes: ['largest-contentful-paint', 'first-input'] })

This snippet shows the basic usage. In real projects you also need to consider error handling and edge cases.

Optimization Tips ​

Building on this, we can optimize further:

javascript
const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.entryType === 'largest-contentful-paint') {
      reportMetric('LCP', entry.startTime)
    }
    if (entry.entryType === 'first-input') {
      reportMetric('FID', entry.processingStart - entry.startTime)
    }
  }
})
observer.observe({ entryTypes: ['largest-contentful-paint', 'first-input'] })

This pattern is very practical in large projects and can significantly reduce maintenance cost.

Summary ​

  • Keep an eye on the community; technical solutions need continuous iteration
  • Don't adopt new technology just for its own sake
  • The code examples are for reference only and should be adapted to your business scenario
  • Frontend image optimization with WebP and AVIF is not a silver bullet; choose based on project scale and tech stack

MIT Licensed