Skip to content

A platform for community discussion. Free, open, simple.

DEV: Fix custom homepage crawler display and override (#31841)

Penar Musarajecbd3cc93ac7
diff --git a/app/controllers/custom_homepage_controller.rb b/app/controllers/custom_homepage_controller.rb
index 2cf241df82b..a2473beffe2 100644
--- a/app/controllers/custom_homepage_controller.rb
+++ b/app/controllers/custom_homepage_controller.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true
class CustomHomepageController < ApplicationController
+ skip_before_action :check_xhr, only: [:index]
+
def index
- render "default/custom"
+ respond_to { |format| format.html { render :index } }
end
end
diff --git a/app/views/custom_homepage/index.erb b/app/views/custom_homepage/index.erb
new file mode 100644
index 00000000000..826673cefa5
--- /dev/null
+++ b/app/views/custom_homepage/index.erb
@@ -0,0 +1,11 @@
+<%= replace_plugin_html('server:custom-homepage-crawler-view') do %>
+ <ul class="crawler-view-anon-menu">
+ <% anonymous_top_menu_items.each_with_index do |item, index| %>
+ <li>
+ <a href='<%= Discourse.base_url %>/<%= item %>'>
+ <%= I18n.t("js.filters.#{item}.title") %>
+ </a>
+ </li>
+ <% end %>
+ </ul>
+<% end %>
diff --git a/app/views/default/custom.html.erb b/app/views/default/custom.html.erb
deleted file mode 100644
index e7363dc51f0..00000000000
--- a/app/views/default/custom.html.erb
+++ /dev/null
@@ -1,9 +0,0 @@
-<ul>
-<% anonymous_top_menu_items.each_with_index do |item, index| %>
- <li>
- <a href='<%= Discourse.base_url %>/<%= item %>'>
- <%= I18n.t("js.filters.#{item}.title") %>
- </a>
- </li>
-<% end %>
-</ul>
\ No newline at end of file
diff --git a/spec/requests/custom_homepage_controller_spec.rb b/spec/requests/custom_homepage_controller_spec.rb
new file mode 100644
index 00000000000..6b9213c2983
--- /dev/null
+++ b/spec/requests/custom_homepage_controller_spec.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+RSpec.describe CustomHomepageController do
+ describe "#index" do
+ context "with crawler view" do
+ it "should display the menu by default" do
+ get "/custom", headers: { "HTTP_USER_AGENT" => "Googlebot" }
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include("<ul class=\"crawler-view-anon-menu\">")
+ end
+
+ context "with plugin override" do
+ let(:plugin_class) do
+ Class.new(Plugin::Instance) do
+ attr_accessor :enabled
+
+ def enabled?
+ @enabled
+ end
+ end
+ end
+
+ it "should allow plugin to override output" do
+ plugin =
+ plugin_class.new(nil, "#{Rails.root}/spec/fixtures/plugins/csp_extension/plugin.rb")
+
+ plugin.register_html_builder("server:custom-homepage-crawler-view") do |c|
+ "<div>override</div>"
+ end
+ plugin.activate!
+ Discourse.plugins << plugin
+ plugin.enabled = true
+
+ get "/custom", headers: { "HTTP_USER_AGENT" => "Googlebot" }
+
+ expect(response.status).to eq(200)
+ expect(response.body).not_to include("<ul class=\"crawler-view-anon-menu\">")
+ expect(response.body).to include("override")
+
+ plugin.enabled = false
+ Discourse.plugins.delete plugin
+ DiscoursePluginRegistry.reset!
+ end
+ end
+ end
+ end
+end