amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

amp-carousel not rendering images

Open uskay opened this issue 6 years ago • 5 comments

What's the issue?

amp-carousel will not render images from the second item and beyond.

How can we reproduce the issue?

  • Repro step:

    1. visit https://belluna-jp.cdn.ampproject.org/c/s/belluna.jp/amp/belluna.html
    2. Press the swipe to the next item button in the carousel next button
    3. <amp-img> is not rendering the image i.e. not adding src / srcset to the <img> tag. no render
  • conditions

    • The page is using <amp-carousel> v0.1
    • Only happens via AMP Cache i.e. when served from origin (https://belluna.jp/amp/belluna.html) works just fine
      • The page has some weird implementation like setting the background-image in the parent div that will never be shown and such... but from the fact that origin AMP works and cached AMP doesn't, I'm assuming this is a AMP Cache SSR problem

What browsers are affected?

Tested in

  • Chrome / Mac OS
  • Safari / Mac OS
  • Chrome / Android
  • Safari / iOS

Which AMP version is affected?

Powered by AMP ⚡ HTML – Version 2001251659540 https://belluna-jp.cdn.ampproject.org/c/s/belluna.jp/amp/belluna.html

uskay avatar Jan 31 '20 02:01 uskay

Copying discussion happening in chat:

If I manually hide the blurry image, it shows But it's not the same amp-img It's like there's something behind it And it's just using the amp-img's responsive size to do it Technically, the amp-img still has width/height of 0x0

.carousel-second {
     background-image: url('https://im-belluna-jp.cdn.ampproject.org/i/s/im.belluna.jp/common/bana/18aw_warm/640_427.jpg');
    background-size: 100%;
}

They're using background images Because the amp-img never actually loads due to whatever they're doing to "move" the carousel

As a stop-gap, they may place a noloading attribute on the img's, which will prevent the blurry image

jridgewell avatar Jan 31 '20 15:01 jridgewell

Based on the non-standard carousel they're using, the easiest solution is to use a noloading attribute on each of the <amp-img>s.

<amp-img noloading src="…"></amp-img>

jridgewell avatar Jan 31 '20 15:01 jridgewell

Because the amp-img never actually loads due to whatever they're doing to "move" the carousel

I should have noticed that. The root cause definitely is not SSR and the site is not using <amp-carousel>. This implementation is very interesting (yet I don't think it's ideal so might be commenting the site owner why simply using <amp-carousel> was not the option).

  • They have hidden input[type=radio].carousel-open input tag just before each div that contains amp-img
  • They have another label tags paired with each of those radio buttons
  • They are controlling the contents opacity and the position when user taps on the label i.e. pseudo css would be: carousel-open:checked+div {opacity: 0; position: static} ... default position is absolute

Weird hack to see enabling this carousel type of experience when there already is a dedicated component (<amp-carousel>) to do that. Also it's more of a selector type implementation so wonder if <amp-selector> could be an alternative. Will talk with the developer.

One thing that I wasn't able to understand was that, even so, why the <amp-img> is not rendering. I built a simple repro site for this:

https://uskay-amp-img-test.glitch.me/

  1. The first sample is how this site is implementing this carousel. It's defaulting the carousel item <div>'s position to absolute and opacity to 0 which would move the div from the current relative position and also hide the div visually. When the user taps on the radio button, it changes the position to static which would bring back the div to its position and also setting the opacity to 1. <amp-img> does not render in this case.

  2. Second one is an alternative implementation I built where it switches the carousel item div's display attribute only. Default to none, when toggled change it to block. <amp-img> does not render in this case as well.

  3. Third has all the carousel item div's position: absolute and switches the opacity. Works, but since it's trying to position the carousel in order with other elements, requires the parent to be position relative and have a dedicated height (In this case I'm adding a responsive div to represent the height).

  4. UX is different this is an example of toggling the <div> by just changing the opacity. <amp-img> renders.

I'm not sure why case 1. and 2. does not work here. Why doesn't it trigger <amp-img> rendering? By the way, if you resize the window, the image will start rendering.

uskay avatar Feb 03 '20 07:02 uskay

I added alternative 5 to https://uskay-amp-img-test.glitch.me/ . It's basically same as 2 but changing the display using <amp-bind>. And it works (I guess <amp-bind> hooks layout change detection). Aside from the ideal implementation for this carousel type experience, I feel this is the easiest way to fix the existing implementation.

So by testing out multiple implementation for this case, I came to an assumption that:

  • <amp-img> does not trigger rendering, or to put it in another word, AMP does not detect the element's layout change when only using CSS (come to think about it, this does make sense because if the JavaScript framework was not monitoring the elements constantly with something like the Mutation Observer, it can never know the change. There's no event to hook.).
  • In this case, the carousel items were initially positioned as absolute and was not being "layouted" anywhere. It's not possible for AMP to detect whether it changed its layout afterwards with just the position change triggered from the :checked pseudo-class selector.

Can anyone confirm this?

uskay avatar Feb 03 '20 08:02 uskay

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 27 '21 12:07 stale[bot]