Skip to content

Commit

Permalink
use Phoenix.VerifiedRoutes.unverified_path/3
Browse files Browse the repository at this point in the history
  • Loading branch information
APB9785 committed Dec 10, 2024
1 parent 7c275a0 commit 0156cc9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
10 changes: 6 additions & 4 deletions lib/beacon/loader/routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ defmodule Beacon.Loader.Routes do
@endpoint.url() <> beacon_media_path(file_name)
end

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

def beacon_sitemap_url do
def beacon_sitemap_url(conn) do
if prefix = @router.__beacon_scoped_prefix_for_site__(@site) do
Path.join([@endpoint.url(), prefix, "sitemap.xml"])
path = Path.join([@endpoint.url(), prefix, "sitemap.xml"])
Phoenix.VerifiedRoutes.unverified_path(conn, conn.private.phoenix_router, path)
end
end

Expand Down
14 changes: 6 additions & 8 deletions lib/beacon/web/controllers/sitemap_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,36 @@ defmodule Beacon.Web.SitemapController 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_index, urls: get_sitemap_urls())
|> render(:sitemap_index, urls: get_sitemap_urls(conn))
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))
|> render(:sitemap, pages: get_pages(conn, site))
end

defp get_sitemap_urls do
defp get_sitemap_urls(conn) do
Beacon.Registry.running_sites()
|> Enum.map(fn site ->
routes_module = Beacon.Loader.fetch_routes_module(site)
Beacon.apply_mfa(site, routes_module, :beacon_sitemap_url, [])
Beacon.apply_mfa(site, routes_module, :beacon_sitemap_url, [conn])
end)
|> Enum.reject(&is_nil/1)
|> Enum.sort()
end

defp get_pages(site) do
defp get_pages(conn, 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]),
loc: Beacon.apply_mfa(site, routes_module, :beacon_page_url, [conn, page]),
lastmod: page.updated_at
}
end)
Expand Down
2 changes: 1 addition & 1 deletion test/beacon_web/controllers/sitemap_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Beacon.Web.SitemapControllerTest do
&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;loc&gt;#{routes.beacon_page_url(conn, page)}&lt;/loc&gt;
&lt;lastmod&gt;#{DateTime.to_string(page.updated_at)}&lt;/lastmod&gt;
&lt;/url&gt;
&lt;/urlset&gt;
Expand Down

0 comments on commit 0156cc9

Please sign in to comment.