Skip to content

Commit 2b6efa5

Browse files
authored
DEV: Add site description to crawler homepage view (#32845)
In some cases, Google crawlers don't output the meta description but rather they output the first bit of text in the UI. Sometimes that is a mix of table headings, topic titles and excerpts, which don't reflect the site's mission. Adding the description to the homepage header might help. Internal ticket t/154372
1 parent b3a39cc commit 2b6efa5

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def use_crawler_layout?
8080
CrawlerDetection.crawler?(request.user_agent, request.headers["HTTP_VIA"])
8181
)
8282
end
83+
helper_method :use_crawler_layout?
8384

8485
def perform_refresh_session
8586
refresh_session(current_user) unless @readonly_mode

app/helpers/application_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ def description_content
288288
)
289289
end
290290

291+
def is_crawler_homepage?
292+
request.path == "/" && use_crawler_layout?
293+
end
291294
# Creates open graph and twitter card meta data
292295
def crawlable_meta_data(opts = nil)
293296
opts ||= {}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<header>
2-
<a href="<%= path "/" %>">
3-
<%=SiteSetting.title%>
4-
</a>
2+
<a href="<%= path "/" %>"><%=SiteSetting.title%></a>
3+
<% if is_crawler_homepage? %>
4+
<p><%= SiteSetting.site_description %></p>
5+
<% end %>
56
</header>

spec/requests/home_page_controller_spec.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# frozen_string_literal: true
22

33
RSpec.describe HomePageController do
4-
describe "#custom" do
4+
describe "homepage" do
55
context "with crawler view" do
6+
before do
7+
SiteSetting.site_description = "This is a test description"
8+
SiteSetting.has_login_hint = false
9+
end
10+
611
it "should display the menu by default" do
712
get "/custom", headers: { "HTTP_USER_AGENT" => "Googlebot" }
813

@@ -43,6 +48,27 @@ def enabled?
4348
DiscoursePluginRegistry.reset!
4449
end
4550
end
51+
52+
it "should display the site description on the homepage" do
53+
get "/", headers: { "HTTP_USER_AGENT" => "Googlebot" }
54+
55+
expect(response.status).to eq(200)
56+
expect(response.body).to include("<p>This is a test description</p>")
57+
expect(response.body).to include(
58+
"<meta name=\"description\" content=\"This is a test description\">",
59+
)
60+
end
61+
62+
it "should not display the site description on another route" do
63+
get "/top", headers: { "HTTP_USER_AGENT" => "Googlebot" }
64+
65+
expect(response.status).to eq(200)
66+
expect(response.body).not_to include("<p>This is a test description</p>")
67+
# but still includes the meta tag
68+
expect(response.body).to include(
69+
"<meta name=\"description\" content=\"This is a test description\">",
70+
)
71+
end
4672
end
4773
end
4874
end

0 commit comments

Comments
 (0)