You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible_web/plugs/no_robots.ex

30 lines
855 B
Elixir

defmodule PlausibleWeb.Plugs.NoRobots do
@moduledoc """
Rejects bot requests by any means available.
We're adding `x-robots-tag` to the response header and annotate the conn
with "noindex, nofollow" under `private.robots` key.
The only exception is, if the request is trying to access our live demo
at plausible.io/plausible.io - in which case we'll allow indexing, but deny
following links and skip the bot detection, in kind robots we trust.
"""
@behaviour Plug
import Plug.Conn
@impl true
def init(opts), do: opts
@impl true
def call(conn, _opts \\ nil) do
conn =
if conn.path_info == ["plausible.io"] do
put_private(conn, :robots, "index, nofollow")
else
put_private(conn, :robots, "noindex, nofollow")
end
put_resp_header(conn, "x-robots-tag", conn.private.robots)
end
end