mirror of
https://github.com/discourse/discourse.git
synced 2025-06-20 23:31:32 +08:00
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
This commit is contained in:
@ -80,6 +80,7 @@ class ApplicationController < ActionController::Base
|
|||||||
CrawlerDetection.crawler?(request.user_agent, request.headers["HTTP_VIA"])
|
CrawlerDetection.crawler?(request.user_agent, request.headers["HTTP_VIA"])
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
helper_method :use_crawler_layout?
|
||||||
|
|
||||||
def perform_refresh_session
|
def perform_refresh_session
|
||||||
refresh_session(current_user) unless @readonly_mode
|
refresh_session(current_user) unless @readonly_mode
|
||||||
|
@ -288,6 +288,9 @@ module ApplicationHelper
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_crawler_homepage?
|
||||||
|
request.path == "/" && use_crawler_layout?
|
||||||
|
end
|
||||||
# Creates open graph and twitter card meta data
|
# Creates open graph and twitter card meta data
|
||||||
def crawlable_meta_data(opts = nil)
|
def crawlable_meta_data(opts = nil)
|
||||||
opts ||= {}
|
opts ||= {}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<header>
|
<header>
|
||||||
<a href="<%= path "/" %>">
|
<a href="<%= path "/" %>"><%=SiteSetting.title%></a>
|
||||||
<%=SiteSetting.title%>
|
<% if is_crawler_homepage? %>
|
||||||
</a>
|
<p><%= SiteSetting.site_description %></p>
|
||||||
|
<% end %>
|
||||||
</header>
|
</header>
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
RSpec.describe HomePageController do
|
RSpec.describe HomePageController do
|
||||||
describe "#custom" do
|
describe "homepage" do
|
||||||
context "with crawler view" 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
|
it "should display the menu by default" do
|
||||||
get "/custom", headers: { "HTTP_USER_AGENT" => "Googlebot" }
|
get "/custom", headers: { "HTTP_USER_AGENT" => "Googlebot" }
|
||||||
|
|
||||||
@ -43,6 +48,27 @@ RSpec.describe HomePageController do
|
|||||||
DiscoursePluginRegistry.reset!
|
DiscoursePluginRegistry.reset!
|
||||||
end
|
end
|
||||||
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("<p>This is a test description</p>")
|
||||||
|
expect(response.body).to include(
|
||||||
|
"<meta name=\"description\" content=\"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).not_to include("<p>This is a test description</p>")
|
||||||
|
# but still includes the meta tag
|
||||||
|
expect(response.body).to include(
|
||||||
|
"<meta name=\"description\" content=\"This is a test description\">",
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user