amp-carousel not rendering images
What's the issue?
amp-carousel will not render images from the second item and beyond.
How can we reproduce the issue?
-
Repro step:
- visit https://belluna-jp.cdn.ampproject.org/c/s/belluna.jp/amp/belluna.html
- Press the swipe to the next item button in the carousel

<amp-img>is not rendering the image i.e. not addingsrc/srcsetto the<img>tag.
-
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-imagein the parentdivthat 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
- The page has some weird implementation like setting the
- The page is using
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
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
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>
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-openinput tag just before eachdivthat containsamp-img - They have another
labeltags paired with each of those radio buttons - They are controlling the contents
opacityand thepositionwhen user taps on thelabeli.e. pseudo css would be:carousel-open:checked+div {opacity: 0; position: static}... default position isabsolute
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/
-
The first sample is how this site is implementing this carousel. It's defaulting the carousel item
<div>'s position toabsoluteand opacity to0which 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 tostaticwhich would bring back the div to its position and also setting theopacityto1.<amp-img>does not render in this case. -
Second one is an alternative implementation I built where it switches the carousel item
div's display attribute only. Default tonone, when toggled change it toblock.<amp-img>does not render in this case as well. -
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 positionrelativeand have a dedicatedheight(In this case I'm adding a responsivedivto represent theheight). -
UX is different this is an example of toggling the
<div>by just changing theopacity.<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.
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 theMutation Observer, it can never know the change. There's no event to hook.).- In this case, the carousel items were initially positioned as
absoluteand was not being "layouted" anywhere. It's not possible for AMP to detect whether it changed its layout afterwards with just thepositionchange triggered from the:checkedpseudo-class selector.
Can anyone confirm this?
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.