# Issue with Onebox and Youtube

**URL:** https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969
**Category:** Bug
**Tags:** onebox
**Created:** [8월 16, 2019, 10:41오전 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969 "2019-08-16T10:41:49Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [8월 16, 2019, 10:41오전 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/1 "2019-08-16T10:41:49Z")

</div>

e.g.

[![](https://global.discourse-cdn.com/meta/original/4X/8/b/3/8b322038609b94fbb17a505909ed189e1aa4158a.jpeg "Why We Chose The Discourse Platform For Our Forums") ](https://www.youtube.com/watch?v=yD1OOz7TDls)

Youtube thumbnail fails to show up on Topic List for e.g. Fakebook theme, TLP plugin.

STR:

Post a youtube video, switch to Fakebook Theme, no thumbnail appears.

Workaround:

Rebuild the post and the thumbnail appears, however, given users are expecting the thumbnail to appear imminently without staff intervention, this is not a great solution.

More info:

`Topic.image_url` appears to be `nil`

This seems to have broken recently, after beginning of August … I think this worked before August the 5th (but not sure when it actually broke). I’ve noticed this aligns roughly with a new version of Onebox …

I tested this on a prior build and the issue was not present, ruling out Youtube og responses I hope. Also Fakebook theme shows the same behaviour as TLP plugin, ruling out TLP plugin, so I do suspect it’s a recent regression in Discourse or Onebox.

This is a real issue for users of Topic List Previews.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [8월 16, 2019, 4:14오후 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/2 "2019-08-16T16:14:25Z")

</div>

Update, I’ve done a Git Bisect, and this seems to be the problem commit:

[https://github.com/discourse/discourse/commit/7c83d2eeb261ac676a8320e6a704752c56fd242e](https://github.com/discourse/discourse/commit/7c83d2eeb261ac676a8320e6a704752c56fd242e)

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [8월 16, 2019, 4:25오후 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/3 "2019-08-16T16:25:12Z")

</div>

```diff
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb
index ac16eb82c16..46050a69e40 100644
--- a/lib/cooked_post_processor.rb
+++ b/lib/cooked_post_processor.rb
@@ -28,7 +28,7 @@ def initialize(post, opts = {})
     @cooking_options = @cooking_options.symbolize_keys
 
     @doc = Nokogiri::HTML::fragment(post.cook(post.raw, @cooking_options))
- @has_oneboxes = post.post_analyzer.found_oneboxes?
+ @has_oneboxes = @doc.css("aside.onebox").count > 0
     @size_cache = {}

```

@nbianca why did we don’t use the `post.post_analyzer.found_oneboxes?` method anymore in here?

All oneboxes which use either oembed or iframe will fail the new CSS selector mode, and that also makes our Onebox detector be different across the codebase.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [8월 17, 2019, 8:56오전 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/4 "2019-08-17T08:56:18Z")

</div>

I agree @nbianca this looks like an incredibly fragile change, why was this method selected?

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [8월 18, 2019, 9:57오전 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/5 "2019-08-18T09:57:41Z")

</div>

The problem was that the `First Onebox` was awarded for any posted link, no matter if it was or was not a Onebox. This happens because PrettyText adds `class="onebox"` to all links and then `Oneboxer` tries to parse all links with class `onebox`.

The change I made checks if the final document has any Oneboxes (Oneboxes are `aside` with class `onebox`). I will have another look and see what and why is it broken and come back with a change that fixes both of the issues.

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [8월 19, 2019, 7:43오후 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/6 "2019-08-19T19:43:35Z")

</div>

I did some improvements here and created [this PR](https://github.com/discourse/discourse/pull/8019).

> [@merefield](#):
>
> `Topic.image_url` appears to be `nil`

I just tested v2.3.2 and it does not look like YouTube links set the `image_url` attribute of the topic. The reason is that the image is extracted from the `src` attribute of an `img`, but when a YouTube video is linked, there is a `<div>` with a `background-image` CSS attribute.

Are you sure `image_url` used to be set?

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [8월 19, 2019, 8:16오후 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/7 "2019-08-19T20:16:25Z")

</div>

Thanks for looking at this. Let me check my end and revert

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [8월 20, 2019, 10:52오전 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/8 "2019-08-20T10:52:51Z")

</div>

@nbianca apologies, this might be a combination of two issues

Works on and prior to this with TLP plugin:

[https://github.com/discourse/discourse/commit/922c40f87cdceb8197dd61361334e0def23f13d5](https://github.com/discourse/discourse/commit/922c40f87cdceb8197dd61361334e0def23f13d5)

After this commit, it fails with TLP.

I agree with you that for YouTube, out of the box that property is not populated.

So it seems like TLP was doing a better job with its overrides, but relying on something which is now changed.

FYI the relevant overrides are here: [https://github.com/angusmcleod/discourse-topic-previews/blob/master/lib/cooked\_post\_processor\_edits.rb](https://github.com/angusmcleod/discourse-topic-previews/blob/master/lib/cooked_post_processor_edits.rb)

I think I will need to do some more analysis, but it looks like there’s been enough of a change to break TLP.

Let me dig into this a little more and revert again. Unfortunately I may not get chance over next couple of days.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [8월 20, 2019, 12:32오후 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/9 "2019-08-20T12:32:03Z")

</div>

I don’t see how that commit can break the TLP plugin 🤔 Are you 100% of your bisect?

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [8월 20, 2019, 12:38오후 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/10 "2019-08-20T12:38:49Z")

</div>

Very odd how the bisect found a change to cooked\_post\_processor though … I will double check and revert.

Yep, confirmed:

With this commit it fails: 7c83d2eeb261ac676a8320e6a704752c56fd242e  
With this prior commit it’s fine: 922c40f87cdceb8197dd61361334e0def23f13d5

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [8월 20, 2019, 2:53오후 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/11 "2019-08-20T14:53:18Z")

</div>

The problem was fixed here:  
[https://github.com/discourse/discourse/pull/8019](https://github.com/discourse/discourse/pull/8019)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [8월 21, 2019, 12:17오전 UTC](https://meta.discourse.org/t/issue-with-onebox-and-youtube/125969/12 "2019-08-21T00:17:14Z")

</div>


