Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Use HTMLImageElement.decode() to pre-decode images #467

Open
andrew-itscript opened this issue May 26, 2021 · 1 comment · May be fixed by #468
Open

New Feature: Use HTMLImageElement.decode() to pre-decode images #467

andrew-itscript opened this issue May 26, 2021 · 1 comment · May be fixed by #468

Comments

@andrew-itscript
Copy link

The decode is a newly added method that you can apply it to an Image object. This method will load, and then decode the image. Both are done in parallel, and does not affect the execution on the main thread. This obviously makes the page to render faster.

The return value of decode is a Promise. When the image decoding is finished successfully, this Promise will be resolved. In case of any image loading error, or decoding error the Promise will fail.

This method supported by most modern browsers and considered in Lazy-loading best practices. For more details you can check https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/decode

The changes are simple enough to add support of it in vue-lazyload:

const loadImageAsync = (item, resolve, reject) => {
  let image = new Image()
  if (!item || !item.src) {
    const err = new Error('image src is required')
    return reject(err)
  }

  image.src = item.src

  if ('decode' in image) {
    image.decode().then(function() {
      resolve({
        naturalHeight: image.naturalHeight,
        naturalWidth: image.naturalWidth,
        src: image.src
      })
    }).catch(function(e) {
      reject(e);
    });
  } else {
    image.onload = function() {
      resolve({
        naturalHeight: image.naturalHeight,
        naturalWidth: image.naturalWidth,
        src: image.src
      })
    }

    image.onerror = function(e) {
      reject(e)
    }
  }
}
@andrew-itscript
Copy link
Author

Largest Contentful Paint (LCP) is a measurement of how long the largest element on the page takes to render. It’s one of several Web Vital metrics that measure how real users perceive the performance of modern web applications. New measurements like Largest Contentful Paint are increasingly important as JavaScript and SPA’s render more content after page load is completed.

Apparently the 'Core Web Vitals' in Google Search Console will become part of the page rank score in SEO soon. Usage of decode method should improve LCP for pages with high resolution images.

@utlime utlime linked a pull request Jun 1, 2021 that will close this issue
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 a pull request may close this issue.

1 participant