Skip to content

DEV: Add site description to crawler homepage view #32845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
DEV: Add site description to crawler homepage view
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.
  • Loading branch information
pmusaraj committed May 21, 2025
commit 535bd8fd43c2bc5dba80f3f7d28e4084ee9b00a3
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def use_crawler_layout?
CrawlerDetection.crawler?(request.user_agent, request.headers["HTTP_VIA"])
)
end
helper_method :use_crawler_layout?

def perform_refresh_session
refresh_session(current_user) unless @readonly_mode
Expand Down
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ def description_content
)
end

def is_crawler_homepage?
request.path == "/" && use_crawler_layout?
end
# Creates open graph and twitter card meta data
def crawlable_meta_data(opts = nil)
opts ||= {}
Expand Down
7 changes: 4 additions & 3 deletions app/views/layouts/_noscript_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<header>
<a href="<%= path "/" %>">
<%=SiteSetting.title%>
</a>
<a href="<%= path "/" %>"><%=SiteSetting.title%></a>
<% if is_crawler_homepage? %>
<p><%= SiteSetting.site_description %></p>
<% end %>
</header>
21 changes: 20 additions & 1 deletion spec/requests/home_page_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true

RSpec.describe HomePageController do
describe "#custom" do
describe "homepage" do
context "with crawler view" do
before do
SiteSetting.site_description = "This is a test description"
SiteSetting.has_login_hint = false
end

it "should display the menu by default" do
get "/custom", headers: { "HTTP_USER_AGENT" => "Googlebot" }

Expand Down Expand Up @@ -43,6 +48,20 @@ def enabled?
DiscoursePluginRegistry.reset!
end
end

it "should display the site description on the homepage" do
get "/", headers: { "HTTP_USER_AGENT" => "Googlebot" }

expect(response.status).to eq(200)
expect(response.body).to include("This is a test description")
end

it "should not display the site description on another route" do
get "/top", headers: { "HTTP_USER_AGENT" => "Googlebot" }

expect(response.status).to eq(200)
expect(response.body).to include("This is a test description")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description says "should not" but test says it should include it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, great catch!

end
end
end
end
Loading