A platform for community discussion. Free, open, simple.
FIX: add og:image:type to OpenGraph tags (#37854)
Régis Hanol1d0bbde98ebf
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 7e1707eac1e..a6f72dc3624 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -328,6 +328,9 @@ module ApplicationHelperresult << tag(:meta, property: "og:image:width", content: opts[:image_width])result << tag(:meta, property: "og:image:height", content: opts[:image_height])end+ if opts[:image_type].present?+ result << tag(:meta, property: "og:image:type", content: opts[:image_type])+ endend%i[url title description].each do |property|diff --git a/app/views/list/list.erb b/app/views/list/list.erbindex 98a6ed71585..60b6c4631bb 100644--- a/app/views/list/list.erb+++ b/app/views/list/list.erb@@ -140,7 +140,14 @@<% if @category %><% content_for :head do %><%= auto_discovery_link_tag(:rss, { action: :category_feed }, rel: 'alternate nofollow', title: t('rss_topics_in_category', category: @category.name)) %>- <%= raw crawlable_meta_data(title: @category.name, description: @category.description_text, image: @category.uploaded_logo&.url.presence, image_width: @category.uploaded_logo&.width, image_height: @category.uploaded_logo&.height) %>+ <%= raw crawlable_meta_data(+ title: @category.name,+ description: @category.description_text,+ image: @category.uploaded_logo&.url.presence,+ image_width: @category.uploaded_logo&.width,+ image_height: @category.uploaded_logo&.height,+ image_type: @category.uploaded_logo&.extension&.then { |ext| MiniMime.lookup_by_extension(ext)&.content_type },+ ) %><% end %><% elsif @tag_name %><% content_for :head do %>diff --git a/app/views/topics/plain.html.erb b/app/views/topics/plain.html.erbindex 67f551c9cbb..87d8e3c5538 100644--- a/app/views/topics/plain.html.erb+++ b/app/views/topics/plain.html.erb@@ -3,7 +3,17 @@<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title><%= @topic_view.topic.title %></title>- <%= raw crawlable_meta_data(title: @topic_view.title, description: @topic_view.summary(strip_images: true), image: @topic_view.image_url, image_width: @topic_view.image_upload&.width, image_height: @topic_view.image_upload&.height, read_time: @topic_view.read_time, like_count: @topic_view.like_count, published_time: @topic_view.published_time) %>+ <%= raw crawlable_meta_data(+ title: @topic_view.title,+ description: @topic_view.summary(strip_images: true),+ image: @topic_view.image_url,+ image_width: @topic_view.image_width,+ image_height: @topic_view.image_height,+ image_type: @topic_view.image_type,+ read_time: @topic_view.read_time,+ like_count: @topic_view.like_count,+ published_time: @topic_view.published_time,+ ) %><% if @topic_view.prev_page %><link rel="prev" href="<%= @topic_view.prev_page_path -%>"><% end %>diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erbindex 92a6bbb8fa7..f762e512ddc 100644--- a/app/views/topics/show.html.erb+++ b/app/views/topics/show.html.erb@@ -148,7 +148,20 @@<% content_for :head do %><%= auto_discovery_link_tag(@topic_view, {action: :feed, slug: @topic_view.topic.slug, topic_id: @topic_view.topic.id}, rel: 'alternate nofollow', title: t('rss_posts_in_topic', topic: @topic_view.title), type: 'application/rss+xml') %>- <%= raw crawlable_meta_data(title: @topic_view.title, description: @topic_view.summary(strip_images: true), image: @topic_view.image_url, image_width: @topic_view.image_upload&.width, image_height: @topic_view.image_upload&.height, read_time: @topic_view.read_time, like_count: @topic_view.like_count, ignore_canonical: true, published_time: @topic_view.published_time, breadcrumbs: @breadcrumbs, tags: @tags.map(&:name)) %>+ <%= raw crawlable_meta_data(+ title: @topic_view.title,+ description: @topic_view.summary(strip_images: true),+ image: @topic_view.image_url,+ image_width: @topic_view.image_width,+ image_height: @topic_view.image_height,+ image_type: @topic_view.image_type,+ read_time: @topic_view.read_time,+ like_count: @topic_view.like_count,+ ignore_canonical: true,+ published_time: @topic_view.published_time,+ breadcrumbs: @breadcrumbs,+ tags: @tags.map(&:name),+ ) %><% if !@topic_view.single_post_request? && (@topic_view.prev_page || @topic_view.next_page) %><% if @topic_view.prev_page %>diff --git a/lib/topic_view.rb b/lib/topic_view.rbindex 8cb23dbc973..51c02a45c2a 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -396,11 +396,20 @@ class TopicViewenddef image_upload- if @post_number == 1- @topic.image_upload- else- desired_post&.image_upload- end+ @image_upload ||= @post_number == 1 ? @topic.image_upload : desired_post&.image_upload+ end++ def image_width+ image_upload&.width+ end++ def image_height+ image_upload&.height+ end++ def image_type+ ext = image_upload&.extension+ MiniMime.lookup_by_extension(ext)&.content_type if ext.present?enddef filter_posts(opts = {})diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rbindex 78e6f2feb1f..99989b04241 100644--- a/spec/helpers/application_helper_spec.rb+++ b/spec/helpers/application_helper_spec.rb@@ -681,6 +681,18 @@ RSpec.describe ApplicationHelper doendend+ context "with opengraph image type" do+ it "includes og:image:type when provided" do+ result = helper.crawlable_meta_data(image: "some-image.png", image_type: "image/png")+ expect(result).to include('<meta property="og:image:type" content="image/png" />')+ end++ it "does not include og:image:type when not provided" do+ result = helper.crawlable_meta_data(image: "some-image.png")+ expect(result).not_to include("og:image:type")+ end+ end+context "with breadcrumbs" dosubject(:metadata) { helper.crawlable_meta_data(breadcrumbs: breadcrumbs) }