Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beacon generates robots.txt per site #700

Open
wants to merge 1 commit into
base: apb/sitemap
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/beacon/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ defmodule Beacon.Router do
get "/__beacon_assets__/js-:md5", Beacon.Web.AssetsController, :js, assigns: %{site: opts[:site]}

get "/sitemap.xml", Beacon.Web.SitemapController, :show, as: :beacon_sitemap, assigns: %{site: opts[:site]}
get "/robots.txt", Beacon.Web.RobotsController, :show, as: :beacon_robots, assigns: %{site: opts[:site]}

live "/*path", Beacon.Web.PageLive, :path
end
Expand Down
20 changes: 20 additions & 0 deletions lib/beacon/web/controllers/robots_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Beacon.Web.RobotsController do
@moduledoc false
use Beacon.Web, :controller

def init(:show), do: :show

def call(%{assigns: %{site: site}} = conn, :show) do
conn
|> put_view(Beacon.Web.RobotsTxt)
|> put_resp_content_type("text/txt")
|> put_resp_header("cache-control", "public max-age=300")
|> render(:robots, sitemap_url: get_sitemap_url(conn, site))
end

defp get_sitemap_url(conn, site) do
routes_module = Beacon.Loader.fetch_routes_module(site)

Beacon.apply_mfa(site, routes_module, :beacon_sitemap_url, [conn])
end
end
3 changes: 3 additions & 0 deletions lib/beacon/web/robots/robots.txt.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# http://www.robotstxt.org
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could disallow the admin prefix here as well. See https://github.com/BeaconCMS/beacon_live_admin/blob/f8ff1621253c30c5f143655c11acf1116fa1c1e4/lib/beacon/live_admin/router.ex#L88

We need to use scoped_path to build the full path including the scope path. So if that attribute is present in the router we could include it as Disallow: <%= @admin_prefix %> otherwise just ignore the line since the admin might not even be included in the router.

And we'll need at least a simple test because that function might change and we need a failing test if that happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could disallow the admin prefix here as well

I don't think that will be included. This PR only uses the beacon_site macro for generation

User-agent: *
Sitemap: <%= @sitemap_url %>
4 changes: 4 additions & 0 deletions lib/beacon/web/robots_txt.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Beacon.Web.RobotsTxt do
import Phoenix.Template, only: [embed_templates: 1]
embed_templates "robots/*.txt"
end
26 changes: 26 additions & 0 deletions test/beacon_web/controllers/robots_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Beacon.Web.RobotsControllerTest do
use Beacon.Web.ConnCase, async: false

test "show", %{conn: conn} do
conn = get(conn, "/robots.txt")

assert response(conn, 200) == """
# http://www.robotstxt.org
User-agent: *
Sitemap: http://localhost:4000/sitemap.xml
"""

assert response_content_type(conn, :txt) =~ "charset=utf-8"

# site: :not_booted
conn = get(conn, "/other/robots.txt")

assert response(conn, 200) == """
# http://www.robotstxt.org
User-agent: *
Sitemap: http://localhost:4000/other/sitemap.xml
"""

assert response_content_type(conn, :txt) =~ "charset=utf-8"
end
end
Loading