Skip to content

Commit

Permalink
robots and noindex nofollow META tags on search actions (DLC-1164)
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Oct 22, 2024
1 parent ee7e29f commit ca1ba46
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ def on_page_not_found
def has_search_parameters?
false
end

def meta_nofollow
@meta_nofollow
end

def meta_nofollow!
@meta_nofollow = true
end

def meta_noindex
@meta_noindex
end

def meta_noindex!
@meta_noindex = true
end
end
3 changes: 3 additions & 0 deletions app/controllers/browse_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class BrowseController < ApplicationController
controller.subsite_layout
}

before_action :meta_nofollow!, only: [:list]
before_action :meta_noindex!, only: [:list]

def initialize(*args)
super(*args)
# _prefixes are where view path lookups are attempted; probably unnecessary
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/repositories/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class CatalogController < Sites::SearchController

before_action :set_map_data_json, only: [:map_search]

before_action :meta_nofollow!, only: [:index, :map_search]
before_action :meta_noindex!, only: [:index, :map_search]

delegate :blacklight_config, to: :@subsite

def initialize(*args)
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/sites/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SearchController < ApplicationController
before_action :store_unless_user, except: [:update, :destroy, :api_info]
before_action :redirect_unless_local, only: :index
before_action :authorize_document, only: :show
before_action :meta_nofollow!, only: [:index, :map_search]
before_action :meta_noindex!, only: [:index, :map_search]

delegate :blacklight_config, to: :@subsite

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/subsites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class SubsitesController < ApplicationController
before_action :default_search_mode_cookie, only: :index
before_action :load_subsite, except: [:home, :page]
before_action :load_page, only: [:home, :index, :page]
before_action :meta_nofollow!, only: [:index, :map_search]
before_action :meta_noindex!, only: [:index, :map_search]
protect_from_forgery :except => [:update, :destroy, :api_info] # No CSRF token required for publishing actions

helper_method :extract_map_data_from_document_list
Expand Down
5 changes: 4 additions & 1 deletion app/views/shared/_head_includes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
<meta name="viewport" content="width=device-width,initial-scale=1.0">

<% # Do not index search result pages %>
<% if query_has_constraints? || (params[:q] && params[:search_field]) %>
<% if controller.meta_noindex %>
<meta name="robots" content="noindex" />
<% end %>
<% if controller.meta_nofollow %>
<meta name="robots" content="nofollow" />
<% end %>
<!--
DLC VERSION: <%= IO.read(Rails.root.to_s+'/VERSION') %> -->
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/browse_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
expect(response.status).to eq(200)
expect(response.body).to include("#{format_link} (#{test_format_count})")
end
it "has robots meta flag attributes" do
get :list, params: { list_id: 'formats' }
expect(controller.instance_variable_get(:@meta_noindex)).to be true
expect(controller.instance_variable_get(:@meta_nofollow)).to be true
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/repositories/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
expect(response.status).to eq(200)
expect(controller.load_subsite.palette).to eql 'monochrome'
end
it "has robots meta flag attributes" do
get :index, params: { repository_id: 'NNC-RB' }
expect(controller.instance_variable_get(:@meta_nofollow)).to be true
expect(controller.instance_variable_get(:@meta_noindex)).to be true
end
end
end
7 changes: 7 additions & 0 deletions spec/features/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
expect(page).to have_text('Perform a search')
end

it "has robots meta tags" do
# <meta name="robots" content="nofollow">
# <meta name="robots" content="noindex">
expect(page).to have_xpath("//meta[@name='robots' and @content='nofollow']", visible: false)
expect(page).to have_xpath("//meta[@name='robots' and @content='noindex']", visible: false)
end

it "shows concepts when performing a search with a relevant query" do
find(:xpath, '//nav[@id="site-banner"]//input[@id="q"]').set('Internal')
find(:xpath, '//nav[@id="site-banner"]//button[@type="submit"]').click
Expand Down

0 comments on commit ca1ba46

Please sign in to comment.