amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

I2D: Deprecation of amp-img component

Open rbeckthomas opened this issue 6 years ago • 17 comments

Summary

The purpose of this document is to describe how we can turn down the usage of the amp-img component in future amp documents.

Design document

Motivation

The amp-img component was primarily created in order to allow for lazy-loading of images in amp pages in order to improve page performance. At the time the native html img tag did not allow for this which necessitated the creation of a custom extension.

Today however, the native img tag does allow for lazy loading through the use of the loading attribute. As a result the amp-img extension is no longer needed to achieve the performance improvements for which it was originally created. Because of this the amp-img extension will need to be constantly updated in order to maintain feature parity with the native html img tag or risk obsolescence.

In the long term we would like to turn down the use of amp-img in new amp documents, instead allowing developers to use the native html img tag with the loading attribute set to lazy in order to achieve these results. This would remove the burden on amp contributors to constantly maintain feature parity and will additionally allow developers to create simple documents without needing to learn the usage of an extra amp extension. Before the deprecation of amp-img several features will need to be implemented in the current amp-img and the current caching system in order to maintain the level functionality which is afforded to us by the use of amp-img.

The two features required are support for native sizing and support for the blurry image placeholder.

Native Sizing Currently we support the usage of the native sizing element if a flag is passed through the amp-img component. In order to ease the transition to native image components we will need to change this flag so that using the native sizing is the default behavior and using the amp-img sizing is done only through the flag.

Blurry Image Placeholder One of the features which amp-img provides which cannot be replicated by native image is the blurry image placeholder. As a result, the best way to maintain this feature after transitioning to native image, will be to include this as a part of the cache transform for images. This change will require coordination with the conduit team.

Additional context

One of the primary challenges we will face with this deprecation is that we will be inherently and intentionally making the behavior of images in amp pages slightly worse. The two main features which will be impacted are image loading behavior and Content Layout Shifting. Due to the way in which native image is implemented there is no way currently to prevent the CLS which will occur once amp-img is deprecated, therefore we will need to very carefully consider how much CLS we are willing to allow in AMP pages and do research into the extent of this problem.

Launch tracker

/cc @ampproject/wg-approvers

rbeckthomas avatar Aug 11 '20 17:08 rbeckthomas

Due to the way in which native image is implemented there is no way currently to prevent the CLS which will occur once amp-img is deprecated, therefore we will need to very carefully consider how much CLS we are willing to allow in AMP pages and do research into the extent of this problem.

How will CLS not be preventable with native images? If an image has a width and height specified, how will it cause any layout shifting? I should think that for an img to be valid AMP, it would require having the following attributes:

  • width and height (unless it can be determined to have fill or flex-item layouts?)
  • loading=lazy (unless per #28954 data-hero is specified?)
  • decoding=async

westonruter avatar Aug 12 '20 20:08 westonruter

One additional feature which may need to be supported is regards to privacy-preserving prerendering on the AMP Cache. Is not amp-img able to delay prerendering if the src is not pointing to the AMP Cache URL, and/or delay rendering if the src is pointing to a 3rd party domain until a amp-consent is accepted (if the amp-img has a data-block-on-consent attribute)? See for example https://github.com/ampproject/amphtml/pull/21752.

westonruter avatar Aug 12 '20 20:08 westonruter

How will CLS not be preventable with native images? If an image has a width and height specified, how will it cause any layout shifting?

The responsive layout seems like it isn't achievable with native img, at least through the current technique.

chasefinch avatar Aug 13 '20 11:08 chasefinch

Wouldn't img with a responsive layout be hidden with the CSS boilerplate until the AMP runtime initializes just the same as amp-img does today? That is, unless the AMP Optimizer has run and in this case the i-amphtml-sizer with the required padding should already be pre-rendered and the CSS boilerplate would not be needed to prevent layout shifting.

Also, to achieve a responsive layout with img: wouldn't this require the amp-layout component anyway? This can be used to make any HTML element responsive.

westonruter avatar Aug 13 '20 16:08 westonruter

A minor concern I'd like to raise is the need to block validation changes on all cache implementations first supporting the changes.

In order to make the validator changes to allow <img> we'll need to first ensure all caches rewrite <img src=> values. At the current time, caches may not rewrite <img src=> values because any <img> tags in a valid document are injected into the document by the cache itself. Before we make a validator change, we'll need to coordinate cache changes. At a minimum, both Google's AMP Cache and Bing's AMP Cache (👋 @ssantosms ) will need to be updated before we proceed. Are there others?

Gregable avatar Aug 13 '20 16:08 Gregable

@westonruter Ah, yes using amp-layout seems like the solution for responsive imges.

chasefinch avatar Aug 13 '20 17:08 chasefinch

I would also recommend using <amp-layout> to wrap an image if you want to apply data-block-on-consent.

zhouyx avatar Aug 14 '20 00:08 zhouyx

The images on my new blog provide:

  • responsive layout
  • srcset
  • zero-extra element blurry placeholder
  • lazy loading
  • async decode
  • no CLS impact in Chrome, Firefox, and Safari Beta

All with exactly one HTML element.

<img 
  alt="…" 
  height="488" 
  src="/img/remote/ZCBwPv.jpg" 
  style="background-size:cover;background-image:url(&quot;data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' xmlns%3Axlink='http%3A//www.w3.org/1999/xlink' viewBox='0 0 9 7'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='.5'%3E%3C/feGaussianBlur%3E%3CfeComponentTransfer%3E%3CfeFuncA type='discrete' tableValues='1 1'%3E%3C/feFuncA%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' xlink%3Ahref='data%3Aimage/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAHCAYAAADam2dgAAAAsUlEQVR4ATXBMW6FMBBAwYdtCF4oEEjQUHP/I3AHOk6Aot+BZBFsNnGRmeI8T00pYa3Fe0+27zsiQt/3NE2DERFSUVAYg7UWVeU4DtZ1JcZIZmJ6qVxJTImUEtk4jizLwr7vZE5RpK4xpuDnedD3pW1bRITrusjcV1nyxEh6wVrL9+dDCIGu6/Dekzn+lM7xb5omRIRt2xiGAWstLoRAjBFVpa5r7vtGVZnnmaqqaJqGX0ECSv3/3qERAAAAAElFTkSuQmCC'%3E%3C/image%3E%3C/svg%3E&quot;)" 
  width="600" 
  decoding="async" 
  loading="lazy" 
  sizes="(max-width: 608px) 100vw, 608px" 
  srcset="/img/remote/ZCBwPv-1920w.jpg 1920w, /img/remote/ZCBwPv-1280w.jpg 1280w, /img/remote/ZCBwPv-640w.jpg 640w, /img/remote/ZCBwPv-320w.jpg 320w">
img {
  max-width: 100%;
  height: auto;
}

The primary difference from AMP is that there is CLS if the width and height are wrong while in AMP you get no CLS but you do get a distorted image.

cramforce avatar Aug 14 '20 09:08 cramforce

@cramforce could you share a link to your demo img? Want to check out.

lannka avatar Aug 14 '20 16:08 lannka

@lannka https://www.industrialempathy.com/posts/design-docs-at-google/

cramforce avatar Aug 17 '20 11:08 cramforce

On the amp.dev homepage, this improved LCP by up to 10% (example here)

kevinkimball avatar Sep 25 '20 20:09 kevinkimball

One thing worth mentioning is tracking the visibility of the image.

Today, <amp-img> visibility is only fired after the image has been loaded. (A signal from AMP component). <amp-analytics> may report visibility too early for <img>.

zhouyx avatar Sep 25 '20 22:09 zhouyx

@zhouyx I think we need to support this as soon as we can. E.g. if a non-AMP component is provided, we should do the element.complete ? Promise.resolve() : listenOncePromise(element, 'load')

dvoytenko avatar Sep 25 '20 22:09 dvoytenko

Agree with @dvoytenko. In general you should expect more of the current AMP components to be implemented as native elements in certain rendering conditions.

kristoferbaxter avatar Sep 25 '20 22:09 kristoferbaxter

Agreed. @dvoytenko is load event a good signal to listen? Does it fire for all native elements, or limited to some elements like img/iframe?

zhouyx avatar Sep 25 '20 23:09 zhouyx

For now you should just specialize on the img tag specifically, imho, and it's img.complete and img.onload/onerror APIs. Unfortunately, there's no clearcut API that spans native elements. But I also think hard-coding the rules for img is totally ok b/c it's such a prevalent case.

dvoytenko avatar Sep 25 '20 23:09 dvoytenko

Sounds good. We can hard code the rule for img and maybe iframe later.

zhouyx avatar Sep 25 '20 23:09 zhouyx