From 2b6efa56f35d45a801ba08fecdb2fe8bcc90ab6f Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Wed, 21 May 2025 18:33:59 -0400 Subject: [PATCH] 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 --- app/controllers/application_controller.rb | 1 + app/helpers/application_helper.rb | 3 +++ app/views/layouts/_noscript_header.html.erb | 7 +++--- spec/requests/home_page_controller_spec.rb | 28 ++++++++++++++++++++- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9952dae0167..ecca328bb37 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -80,6 +80,7 @@ class ApplicationController < ActionController::Base 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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fc49b2788ad..c05ba27ddad 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -288,6 +288,9 @@ module ApplicationHelper ) 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 ||= {} diff --git a/app/views/layouts/_noscript_header.html.erb b/app/views/layouts/_noscript_header.html.erb index 61095d320bd..43567d2a069 100644 --- a/app/views/layouts/_noscript_header.html.erb +++ b/app/views/layouts/_noscript_header.html.erb @@ -1,5 +1,6 @@
- "> - <%=SiteSetting.title%> - + "><%=SiteSetting.title%> + <% if is_crawler_homepage? %> +

<%= SiteSetting.site_description %>

+ <% end %>
diff --git a/spec/requests/home_page_controller_spec.rb b/spec/requests/home_page_controller_spec.rb index 6ab95889d28..3bfb65738c4 100644 --- a/spec/requests/home_page_controller_spec.rb +++ b/spec/requests/home_page_controller_spec.rb @@ -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" } @@ -43,6 +48,27 @@ RSpec.describe HomePageController do 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

") + expect(response.body).to include( + "", + ) + 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("

This is a test description

") + # but still includes the meta tag + expect(response.body).to include( + "", + ) + end end end end