-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
test/beacon_web/controllers/media_library_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) == """ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>#{routes.beacon_page_url(page)}</loc> | ||
<lastmod>#{DateTime.to_string(page.updated_at)}</lastmod> | ||
</url> | ||
</urlset> | ||
""" | ||
assert response_content_type(conn, :xml) =~ "charset=utf-8" | ||
end | ||
end |