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:


`/lib/dev_decks_web/router.ex` ```elixir scope "/", DevDecksWeb do pipe_through :browser

get “/sitemap.xml”, PageController, :sitemap end

defmodule DevDecksWeb.PageController do
  use DevDecksWeb, :controller

  def sitemap(conn, _params) do
    |> put_resp_content_type("text/xml")
    |> render("sitemap.xml")


`/lib/dev_decks_web/templates/page/sitemap.xml.eex` ```elixir 2020-12-22T18:58:52+01:00 1.0

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.