[amp-story-player] player doesn't load when creating player from javascript
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/
- open above URL and see code to understand problem
- 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?
-
take a look at this: https://examples.tappable.co/bonappetit
@jaygray0919 Thanks but this is not what I'm asking.
@Enriqe Can you please help me with this issue?
Yep, I'll take a look in a bit.
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.