FullstackPhoenix

XML Sitemaps for Phoenix

phoenixseositemapmarketing

You publish a new comparison page on your marketing site. A week later, the support team asks why it isn't ranking — and then realises Google has not crawled it yet, because nothing on the site links to it from a high-traffic page. A sitemap is the cheapest fix: it gives crawlers a canonical list of URLs to discover without depending on internal link structure.

This feature wires a real sitemap into your Phoenix application. It is not a generator that introspects your routes; it is the plumbing — controller, view, router entry, template, robots.txt, and a test — that puts your sitemap at /sitemap.xml so you can keep adding URLs to it as the marketing surface grows.

Help search engines find your marketing surface

A sitemap is not magic SEO. It is the basic hygiene step that makes the rest of your SEO work pay off. Without one, search engines find your pages only when something else links to them; with one, they find them as fast as the crawl budget allows.

What This Feature Adds

  • An inject_after on lib/my_app_web.ex that adds :xml to the list of allowed formats
  • A SitemapController with an index action
  • A SitemapXML view module using embed_templates to render the XML
  • An embed_templates template at lib/my_app_web/controllers/sitemap_xml/index.xml.eex (with content-type and layout handling appropriate for XML)
  • A robots.txt referencing the sitemap URL
  • A router entry mounting /sitemap.xml at the controller
  • A controller test ensuring the sitemap endpoint stays valid across changes

How It Fits Into Your Phoenix Application

The XML template uses your existing Phoenix URL helpers, which means the sitemap respects your configured host and scheme without any extra wiring:

<url>
<loc><%= url(~p"/") %></loc>
<changefreq>daily</changefreq>
<priority>1</priority>
</url>

You expand the template by adding more <url> entries — either statically for marketing pages, or dynamically by querying public content from your database. Because the template is a regular embedded template, you can also call out to a context module that returns the list of URLs to emit.

Installation Notes

  • No feature dependencies. Sitemap stands on its own.
  • After install, update the robots.txt domain placeholder so it points at your production host. The shipped file references example.com.
  • The shipped template renders the homepage. Add real URL entries before submitting the sitemap to Google Search Console.
  • Submit https://your-domain/sitemap.xml in Google Search Console (and Bing, if relevant) once the file is live in production.

Build This With Live SaaS Kit

Install saas_kit, then mix saaskit.feature.install sitemap to add a real /sitemap.xml and stop hoping Google finds your new pages by accident.