Add a sitemap to a Phoenix project
This explains what I had to do in my own Phoenix project to render a /sitemap.xml
file for Google to more effectively crawl my website.
First I had to make sure all of the content I wanted crawled existed under the URLs I expected and then used a sitemap generator to create the sitemap.xml file. I just had to enter the live URL of my project and it produced the downloadable file.
After that, in my app, I added a root.xml.eex
template in templates/layouts
so my app has a place to find root xml rendering. File contents:
/lib/dev_decks_web/templates/layout/root.xml.eex
<%= @inner_content %>
From there I used the code I had written in my app for handling static pages to add the route, controller action and sitemap template. The contents of the sitemap template are a copy and paste from the sitemap generator file:
Route
`/lib/dev_decks_web/router.ex` ```elixir scope "/", DevDecksWeb do pipe_through :browserget “/sitemap.xml”, PageController, :sitemap end
<h3>Controller</h3>
`/lib/dev_decks_web/controllers/page_controller.ex`
```elixir
defmodule DevDecksWeb.PageController do
use DevDecksWeb, :controller
def sitemap(conn, _params) do
conn
|> put_resp_content_type("text/xml")
|> render("sitemap.xml")
end
end
Template
`/lib/dev_decks_web/templates/page/sitemap.xml.eex` ```elixir…
After that I was able to route to `/sitemap.xml` in the browser and see the contents of my XML file and Google has since been able to do the same.