analytics/lib/plausible_web/plugs/no_robots.ex
hq1 fea9bb32ee
Prevent robots from looking too much into public dashboards (#3118)
* Add noindex,nofollow to dashboard pages

* Implement NoRobots plug

* Enable NoRobots plug in the router

* Fixup internal route

* Fix double slash in the router

* Add special bot treatment to plausible.io live demo page

* Revert aggressive protection with agent detection
2023-07-11 10:47:03 +02:00

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