Skip to content

Commit

Permalink
per-site sitemaps via beacon_site
Browse files Browse the repository at this point in the history
  • Loading branch information
APB9785 committed Dec 4, 2024
1 parent ff30a1c commit 5a4fb2e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lib/beacon/loader/routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ defmodule Beacon.Loader.Routes do
@endpoint.url() <> beacon_media_path(file_name)
end

def beacon_page_url(%{path: path} = page) do
prefix = @router.__beacon_scoped_prefix_for_site__(@site)
Path.join([@endpoint.url(), prefix, path])
end

defp sanitize_path(path) do
String.replace(path, "//", "/")
end
Expand Down
2 changes: 2 additions & 0 deletions lib/beacon/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ defmodule Beacon.Router do
get "/__beacon_assets__/css-:md5", Beacon.Web.AssetsController, :css, assigns: %{site: opts[:site]}
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]}

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

def init(action) when action in [:index, :show], do: action

# def call(%{assigns: %{sites: _sites}} = _conn, :index) do
# # TODO render embedded sitemap_index.xml.eex
# end

def call(%{assigns: %{site: site}} = conn, :show) do
conn
|> put_view(Beacon.Web.SitemapXML)
|> put_resp_content_type("text/xml")
# may need to adjust caching
|> put_resp_header("cache-control", "public max-age=300")
|> render(:sitemap, pages: get_pages(site))
end

defp get_pages(site) do
routes_module = Beacon.Loader.fetch_routes_module(site)

site
|> Beacon.Content.list_published_pages()
|> Enum.map(fn page ->
%{
loc: Beacon.apply_mfa(site, routes_module, :beacon_page_url, [page]),
lastmod: page.updated_at
}
end)
end
end
7 changes: 7 additions & 0 deletions lib/beacon/web/sitemap/sitemap.xml.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<%= for page <- @pages do %><url>
<loc><%= page.loc %></loc>
<lastmod><%= page.lastmod %></lastmod>
</url><% end %>
</urlset>
4 changes: 4 additions & 0 deletions lib/beacon/web/sitemap_xml.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Beacon.Web.SitemapXML do
import Phoenix.Template, only: [embed_templates: 1]
embed_templates "sitemap/*.xml"
end
6 changes: 0 additions & 6 deletions test/beacon_web/controllers/media_library_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
defmodule Beacon.Web.Controllers.MediaLibraryControllerTest do
use Beacon.Web.ConnCase, async: true

setup do
Process.flag(:error_handler, Beacon.ErrorHandler)
Process.put(:__beacon_site__, :my_site)
:ok
end

test "show", %{conn: conn} do
%{file_name: file_name} = Beacon.Test.Fixtures.beacon_media_library_asset_fixture(site: :my_site)
routes = Beacon.Loader.fetch_routes_module(:my_site)
Expand Down
33 changes: 33 additions & 0 deletions test/beacon_web/controllers/sitemap_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule Beacon.Web.SitemapControllerTest do
use Beacon.Web.ConnCase, async: false

test "show", %{conn: conn} do
site = :my_site

layout =
beacon_published_layout_fixture(
site: site,
template: """
<header>Page header</header>
<%= @inner_content %>
<footer>Page footer</footer>
"""
)

page = beacon_published_page_fixture(site: site, path: "/foo", layout_id: layout.id)

routes = Beacon.Loader.fetch_routes_module(site)
conn = get(conn, "/sitemap.xml")

assert response(conn, 200) == """
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;urlset xmlns=&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot;&gt;
&lt;url&gt;
&lt;loc&gt;#{routes.beacon_page_url(page)}&lt;/loc&gt;
&lt;lastmod&gt;#{DateTime.to_string(page.updated_at)}&lt;/lastmod&gt;
&lt;/url&gt;
&lt;/urlset&gt;
"""
assert response_content_type(conn, :xml) =~ "charset=utf-8"
end
end

0 comments on commit 5a4fb2e

Please sign in to comment.