Problem/Motivation

Currently on both the video formatter and the remote formatter, the "poster" setting doesn't properly work. The Poster appears for one second and disappears again.

And even when it appears, it doesn't seem to fit the player correctly.

Steps to reproduce

Proposed resolution

Fix formatter "poster" setting

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#5 videojs-poster-img-slot.patch5.28 KBluwes

Issue fork videojs-3618103

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

grevil created an issue. See original summary.

grevil’s picture

grevil’s picture

Priority: Normal » Major
anybody’s picture

I think we should see this also as alpha blocker.

luwes’s picture

StatusFileSize
new5.28 KB

ai;dr

Hey,

The issue is that the media-poster component isn't needed in the twig template, it's only needed in an ejected skin.
The best performance will be to use a plain img tag there with a `slot="poster"` attribute so it projects it in the skin.
More info below by AI.

---

I think I found it, and it isn't the skin — the poster image just never reaches
the skin's poster slot.

Both components wrap the poster in a `` of their own:

```twig

{% block poster_content %}
{{ poster }}
{% endblock %}

```

But the skins already carry one. From `@videojs/html` 10.0.0-beta.31,
`src/define/video/skin.ts` (and `minimal-skin.ts`, which is identical here):

```html




Only local images are allowed.


```

And the `PosterElement` docblock states the contract outright:

> `` — sets `src` on a poster image it does not own. […] Renders
> no image of its own, so include one: `Only local images are allowed.`.
> **Inside a skin, an `Only local images are allowed.` of yours replaces the one the skin
> carries.**

(Both quotes are verifiable from the CDN: `cdn/video.dev.js` and the
`sourcesContent` of `cdn/poster-element-BA7p_OJL.js.map`.)

Our wrapper has no `slot` attribute, so it is assigned to the skin's **default**
slot, landing beside the media element instead of inside the skin's
``. That accounts for both symptoms:

- **"doesn't fit the player"** — the positioning lives in the skin's shadow CSS
as `.media-default-skin media-poster { position: absolute; inset: 0; … }` and
`.media-default-skin media-poster ::slotted(img) { position: absolute; inset: 0;
object-fit: … }`. A descendant selector inside a shadow root cannot match a
light DOM node assigned to a slot — only `::slotted()` reaches it — so our
wrapper gets none of that and the image renders at natural size, in flow.
- **"appears for one second and disappears"** — the `Only local images are allowed.` paints during
parsing, then `media-poster` upgrades and applies its own visibility state
(`PosterCore.getState()` returns `visible: !media.started`, written out by
`applyStateDataAttrs`). We now have two PosterElements bound to the same
player state, and the one the skin's CSS actually styles is not ours. I am
fairly confident of the mechanism but have not stepped through it line by
line — the slotting problem above is the part I have verified.

## Fix (patch attached)

1. Drop the `` wrapper from `video-player.twig` and
`video-player-remote.twig`; print the poster straight into the skin.
2. `VideoJsVideoPlayerFormatterTrait::getPoster()`: add `slot="poster"` and
`decoding="async"` (matching the placeholder the skin ships) to the image
render array.

I put the attribute in `getPoster()` rather than the shared
`getImageFromSettings()` deliberately — the latter also builds the
click-through thumbnail for `videojs_trigger_link`, which must not be slotted.

Resulting markup:

```html


src="https://stream.mux.com/…m3u8">
Only local images are allowed.


```

Two side notes:

- The wrapper is currently emitted whenever `block('poster_content')` is not
empty, and a whitespace-only Twig block is not empty — so an empty
`` is rendered even when the poster setting is
"Disabled". Removing the wrapper takes care of that too.
- A slotted `Only local images are allowed.` is plain markup, so the preload scanner starts the poster
request during HTML parsing rather than after the ES modules load and upgrade
the custom elements. Nice win for LCP on poster-led pages.

The `poster_content` slot still works, but an override now has to carry
`slot="poster"` itself; I documented that in both Twig docblocks. If you would
rather the component guarantee it, the alternative is keeping a wrapper and
putting `slot="poster"` on it — that works, but ships a second ``
where upstream expects just an `Only local images are allowed.`.

Tested on Drupal 11.4.5 / PHP 8.4 against 10.x at 8ead389, CDN library, both
`default` and `minimal` skins. Happy to turn this into an MR if you create an
issue fork.

thomas.frobieter’s picture

Assigned: Unassigned » thomas.frobieter

Thanks a lot, @luwes! Great to have a library developer here.

So let's get this ready..

thomas.frobieter’s picture

Assigned: thomas.frobieter » grevil
Status: Active » Needs review
grevil’s picture

Assigned: grevil » Unassigned
Status: Needs review » Reviewed & tested by the community

All green, except the phpunit test, but that is still unrelated and should be fixed in a follow-up issue. LGTM!

grevil’s picture

Status: Reviewed & tested by the community » Fixed

Thanks all!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.