amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

[amp-story-player] player doesn't load when creating player from javascript

Open saurabh230197 opened this issue 5 years ago • 5 comments

What's the issue?

When we get stories list from a server and creating player from JavaScript the player doesn't loads. player script doesn't check when a player added in DOM after some time of content init.

How do we reproduce the issue?

https://jsfiddle.net/dz1yxbct/

  1. open above URL and see code to understand problem
  2. See the player doesn't load in preview section.

note - added setTimeout in JavaScript because assuming that server will respond stories list after sometime.

What browsers are affected?

All browsers

Which AMP version is affected?

-

saurabh230197 avatar Dec 25 '20 13:12 saurabh230197

take a look at this: https://examples.tappable.co/bonappetit

jaygray0919 avatar Dec 26 '20 23:12 jaygray0919

@jaygray0919 Thanks but this is not what I'm asking.

saurabh230197 avatar Dec 29 '20 04:12 saurabh230197

@Enriqe Can you please help me with this issue?

saurabh230197 avatar Jan 06 '21 07:01 saurabh230197

Yep, I'll take a look in a bit.

Enriqe avatar Jan 14 '21 16:01 Enriqe

Instead of adding stories by appending anchor tags to the DOM, you should use the add() API on the player

https://github.com/ampproject/amphtml/blob/master/spec/amp-story-player.md#add

taking your example, this would look like:

        const stories = [
          {
            "href":"https://style.visualstories.com/quizzes/test-your-knowledge-about-hip-hop-fashion"
          },
          {
            "href":"https://www.vacaytaste.com/best-things-to-do-in-bora-bora"
          }
        ];

        const playerContainer = document.getElementById("story-player");
        const playerEl = document.createElement("amp-story-player");
        playerContainer.append(playerEl);
        const player = new AmpStoryPlayer(window, playerEl);
        player.load();

        if (playerEl.isReady) {
            playerEl.add(stories);
        } else {
          playerEl.addEventListener('ready', () => {
            playerEl.add(stories);
          });
        }

      };

If there are some exceptions, it might be because there were some weird race conditions that were fixed by #32153, so just opt-in to the nightly channel to test it out.

Enriqe avatar Feb 03 '21 20:02 Enriqe