diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 27810f9e718..c6cfcf31652 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -44,6 +44,7 @@ class ApplicationController < ActionController::Base after_action :perform_refresh_session after_action :dont_cache_page after_action :conditionally_allow_site_embedding + after_action :add_noindex_header, if: -> { is_feed_request? } layout :set_layout @@ -801,6 +802,14 @@ class ApplicationController < ActionController::Base request.env['DISCOURSE_IS_ASSET_PATH'] = 1 end + def is_feed_request? + request.format.atom? || request.format.rss? + end + + def add_noindex_header + response.headers['X-Robots-Tag'] = 'noindex' + end + protected def render_post_json(post, add_raw: true) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 774b7ed82ce..40c84cad799 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -47,6 +47,8 @@ class UsersController < ApplicationController :admin_login, :confirm_admin] + after_action :add_noindex_header, only: [:show] + def index end @@ -73,8 +75,6 @@ class UsersController < ApplicationController track_visit_to_user_profile end - response.headers['X-Robots-Tag'] = 'noindex' - # This is a hack to get around a Rails issue where values with periods aren't handled correctly # when used as part of a route. if params[:external_id] && params[:external_id].ends_with?('.json') diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb index 3897f03abcb..5172969289f 100644 --- a/spec/requests/list_controller_spec.rb +++ b/spec/requests/list_controller_spec.rb @@ -300,6 +300,7 @@ RSpec.describe ListController do get "/latest.rss" expect(response.status).to eq(200) expect(response.media_type).to eq('application/rss+xml') + expect(response.headers['X-Robots-Tag']).to eq('noindex') end it 'renders links correctly with subfolder' do