Skip to content

Commit cdda2ea

Browse files
committed
Robots, begone
* robots.txt: "Please, don't come in." If a page is directly linked, the URL can still appear in search results, though. * X-Robots-Tag: "If you're here, forget what you saw." Works even if the crawler ignores robots.txt or reaches a page via external link. Can remove already-indexed pages. * Public boards may be indexed. They're meant to be shareable and discoverable, not just private link sharing.
1 parent f64d584 commit cdda2ea

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class ApplicationController < ActionController::Base
22
include Authentication
33
include Authorization
4+
include BlockSearchEngineIndexing
45
include CurrentRequest, CurrentTimezone, SetPlatform
56
include RequestForgeryProtection
67
include TurboFlash, ViewTransitions
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Tell crawlers like Googlebot to drop pages entirely from search results, even
2+
# if other sites link to it
3+
module BlockSearchEngineIndexing
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
after_action :block_search_engine_indexing
8+
end
9+
10+
private
11+
def block_search_engine_indexing
12+
headers["X-Robots-Tag"] = "none"
13+
end
14+
end

app/controllers/public/base_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Public::BaseController < ApplicationController
22
allow_unauthenticated_access
3+
skip_after_action :block_search_engine_indexing
34

45
before_action :set_board, :set_card, :set_public_cache_expiration
56

public/robots.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
1+
User-Agent: *
2+
Allow: /public/
3+
Disallow: /
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "test_helper"
2+
3+
class BlockSearchEngineIndexingTest < ActionDispatch::IntegrationTest
4+
test "sets X-Robots-Tag header to none on authenticated requests" do
5+
sign_in_as :david
6+
7+
get board_path(boards(:writebook))
8+
assert_response :success
9+
assert_equal "none", response.headers["X-Robots-Tag"]
10+
end
11+
12+
test "sets X-Robots-Tag header to none on unauthenticated requests" do
13+
untenanted do
14+
get new_session_path
15+
end
16+
17+
assert_response :success
18+
assert_equal "none", response.headers["X-Robots-Tag"]
19+
end
20+
21+
test "does not set X-Robots-Tag header on public board pages" do
22+
boards(:writebook).publish
23+
24+
get public_board_path(boards(:writebook).publication.key)
25+
assert_response :success
26+
assert_nil response.headers["X-Robots-Tag"]
27+
end
28+
end

0 commit comments

Comments
 (0)