berrypod/lib/berrypod_web/controllers/seo_controller.ex
jamey 0f1135256d add canonical URLs, robots.txt, and sitemap.xml
Canonical: all shop pages now assign og_url (reusing the existing og:url
assign), which the layout renders as <link rel="canonical">. Collection
pages strip the sort param so ?sort=price_asc doesn't create a duplicate
canonical.

robots.txt: dynamic controller disallows /admin/, /api/, /users/,
/webhooks/, /checkout/. Removed robots.txt from static_paths so it
goes through the router instead of Plug.Static.

sitemap.xml: auto-generated from all visible products + categories +
static pages, served as application/xml. 8 tests.

Also updates PROGRESS.md: marks tasks 55, 58, 59, 61, 62 as done.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-23 21:47:35 +00:00

74 lines
1.7 KiB
Elixir

defmodule BerrypodWeb.SeoController do
use BerrypodWeb, :controller
alias Berrypod.Products
def robots(conn, _params) do
base = BerrypodWeb.Endpoint.url()
content = """
User-agent: *
Allow: /
Disallow: /admin/
Disallow: /api/
Disallow: /users/
Disallow: /webhooks/
Disallow: /checkout/
Sitemap: #{base}/sitemap.xml
"""
conn
|> put_resp_content_type("text/plain")
|> send_resp(200, content)
end
def sitemap(conn, _params) do
base = BerrypodWeb.Endpoint.url()
products = Products.list_visible_products()
categories = Products.list_categories()
static_pages = [
{"/", "daily", "1.0"},
{"/collections/all", "daily", "0.9"},
{"/about", "monthly", "0.5"},
{"/contact", "monthly", "0.5"},
{"/delivery", "monthly", "0.5"},
{"/privacy", "monthly", "0.3"},
{"/terms", "monthly", "0.3"}
]
category_pages =
Enum.map(categories, fn cat ->
{"/collections/#{cat.slug}", "daily", "0.8"}
end)
product_pages =
Enum.map(products, fn product ->
{"/products/#{product.slug}", "weekly", "0.9"}
end)
all_pages = static_pages ++ category_pages ++ product_pages
entries =
Enum.map_join(all_pages, "\n", fn {path, changefreq, priority} ->
" <url>\n" <>
" <loc>#{base}#{path}</loc>\n" <>
" <changefreq>#{changefreq}</changefreq>\n" <>
" <priority>#{priority}</priority>\n" <>
" </url>"
end)
xml = """
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
#{entries}
</urlset>
"""
conn
|> put_resp_content_type("application/xml")
|> send_resp(200, xml)
end
end