From 09f1ef6b05a15b552ad022b43187c813d5ea3a90 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Tue, 12 Jul 2022 20:52:55 +0300 Subject: [PATCH] DEV: Add plugin API to add to robots.txt (#17378) This plugin API can be used to add to robots.txt. The event handler receives the complete robots information before it is converted into robots.txt. --- app/controllers/robots_txt_controller.rb | 2 ++ spec/requests/robots_txt_controller_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/controllers/robots_txt_controller.rb b/app/controllers/robots_txt_controller.rb index 4bc2c449d34..706a67be772 100644 --- a/app/controllers/robots_txt_controller.rb +++ b/app/controllers/robots_txt_controller.rb @@ -84,6 +84,8 @@ class RobotsTxtController < ApplicationController result[:agents] << { name: 'Googlebot', disallow: deny_paths_googlebot } end + DiscourseEvent.trigger(:robots_info, result) + result end end diff --git a/spec/requests/robots_txt_controller_spec.rb b/spec/requests/robots_txt_controller_spec.rb index 65fc58e05aa..a241cb2c6f2 100644 --- a/spec/requests/robots_txt_controller_spec.rb +++ b/spec/requests/robots_txt_controller_spec.rb @@ -161,5 +161,25 @@ RSpec.describe RobotsTxtController do expect(response.body).not_to include(sitemap_line) end end + + describe 'plugins' do + let(:event_handler) do + Proc.new { |robots_info| robots_info[:agents] << { name: 'Test', disallow: ['/test/'] } } + end + + before do + DiscourseEvent.on(:robots_info, &event_handler) + end + + after do + DiscourseEvent.off(:robots_info, &event_handler) + end + + it 'can add to robots.txt' do + get '/robots.txt' + + expect(response.parsed_body).to include("User-agent: Test\nDisallow: /test/") + end + end end end