A platform for community discussion. Free, open, simple.
DEV: Add plugin API to add to robots.txt (#17378)
Bianca Nenciu09f1ef6b05a1
diff --git a/app/controllers/robots_txt_controller.rb b/app/controllers/robots_txt_controller.rbindex 4bc2c449d34..706a67be772 100644--- a/app/controllers/robots_txt_controller.rb+++ b/app/controllers/robots_txt_controller.rb@@ -84,6 +84,8 @@ class RobotsTxtController < ApplicationControllerresult[:agents] << { name: 'Googlebot', disallow: deny_paths_googlebot }end+ DiscourseEvent.trigger(:robots_info, result)+resultendenddiff --git a/spec/requests/robots_txt_controller_spec.rb b/spec/requests/robots_txt_controller_spec.rbindex 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 doexpect(response.body).not_to include(sitemap_line)endend++ 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+ endendend