Skip to content

A platform for community discussion. Free, open, simple.

FIX: add og:image:width and og:image:height to OpenGraph tags (#37849)

Régis Hanol55e992f50787
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index e4c65c1fdc4..7e1707eac1e 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -322,7 +322,13 @@ module ApplicationHelper
generate_twitter_card_metadata(result, opts)
- result << tag(:meta, property: "og:image", content: opts[:image]) if opts[:image].present?
+ if opts[:image].present?
+ result << tag(:meta, property: "og:image", content: opts[:image])
+ if opts[:image_width].present? && opts[:image_height].present?
+ result << tag(:meta, property: "og:image:width", content: opts[:image_width])
+ result << tag(:meta, property: "og:image:height", content: opts[:image_height])
+ end
+ end
%i[url title description].each do |property|
if opts[property].present?
diff --git a/app/views/list/list.erb b/app/views/list/list.erb
index ce56aa71fe8..98a6ed71585 100644
--- a/app/views/list/list.erb
+++ b/app/views/list/list.erb
@@ -140,7 +140,7 @@
<% 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) %>
+ <%= 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) %>
<% end %>
<% elsif @tag_name %>
<% content_for :head do %>
diff --git a/app/views/topics/plain.html.erb b/app/views/topics/plain.html.erb
index 25531cb837d..67f551c9cbb 100644
--- a/app/views/topics/plain.html.erb
+++ b/app/views/topics/plain.html.erb
@@ -3,7 +3,7 @@
<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, 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_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) %>
<% 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.erb
index 8b47cabe4ed..92a6bbb8fa7 100644
--- a/app/views/topics/show.html.erb
+++ b/app/views/topics/show.html.erb
@@ -148,7 +148,7 @@
<% 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, 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_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)) %>
<% if !@topic_view.single_post_request? && (@topic_view.prev_page || @topic_view.next_page) %>
<% if @topic_view.prev_page %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb
index 007a600d547..c0e5a735af5 100644
--- a/app/views/users/show.html.erb
+++ b/app/views/users/show.html.erb
@@ -9,9 +9,9 @@
<% content_for :head do %>
<% if @restrict_fields %>
- <%= raw crawlable_meta_data(title: @user.username, image: @user.small_avatar_url) %>
+ <%= raw crawlable_meta_data(title: @user.username, image: @user.small_avatar_url, image_width: 45, image_height: 45) %>
<% else %>
- <%= raw crawlable_meta_data(title: @user.username, description: @user.user_profile.bio_summary, image: @user.small_avatar_url) %>
+ <%= raw crawlable_meta_data(title: @user.username, description: @user.user_profile.bio_summary, image: @user.small_avatar_url, image_width: 45, image_height: 45) %>
<% end %>
<% end %>
diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index a38b09b523a..8cb23dbc973 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -395,6 +395,14 @@ class TopicView
desired_post&.image_url
end
+ def image_upload
+ if @post_number == 1
+ @topic.image_upload
+ else
+ desired_post&.image_upload
+ end
+ end
+
def filter_posts(opts = {})
if opts[:post_number].present?
filter_posts_near(opts[:post_number].to_i)
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index d34bda53fd3..78e6f2feb1f 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -666,6 +666,21 @@ RSpec.describe ApplicationHelper do
end
end
+ context "with opengraph image dimensions" do
+ it "includes og:image:width and og:image:height when provided" do
+ result =
+ helper.crawlable_meta_data(image: "some-image.png", image_width: 1024, image_height: 768)
+ expect(result).to include('<meta property="og:image:width" content="1024" />')
+ expect(result).to include('<meta property="og:image:height" content="768" />')
+ end
+
+ it "does not include og:image dimensions when not provided" do
+ result = helper.crawlable_meta_data(image: "some-image.png")
+ expect(result).not_to include("og:image:width")
+ expect(result).not_to include("og:image:height")
+ end
+ end
+
context "with breadcrumbs" do
subject(:metadata) { helper.crawlable_meta_data(breadcrumbs: breadcrumbs) }