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>
This commit is contained in:
jamey
2026-02-23 21:47:35 +00:00
parent b11f7d47d0
commit 0f1135256d
16 changed files with 2144 additions and 13 deletions

View File

@@ -28,6 +28,7 @@
<meta property="og:description" content={og_description} />
<meta property="og:type" content={assigns[:og_type] || "website"} />
<%= if assigns[:og_url] do %>
<link rel="canonical" href={assigns[:og_url]} />
<meta property="og:url" content={assigns[:og_url]} />
<% end %>
<%= if assigns[:og_image] do %>

View File

@@ -0,0 +1,73 @@
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

View File

@@ -32,6 +32,7 @@ defmodule BerrypodWeb.Shop.Collection do
socket
|> assign(:page_title, title)
|> assign(:page_description, collection_description(title))
|> assign(:og_url, BerrypodWeb.Endpoint.url() <> "/collections/#{slug}")
|> assign(:collection_title, title)
|> assign(:current_category, category)
|> assign(:current_sort, sort)

View File

@@ -6,7 +6,11 @@ defmodule BerrypodWeb.Shop.Contact do
{:ok,
socket
|> assign(:page_title, "Contact")
|> assign(:page_description, "Get in touch with us for any questions or help with your order.")}
|> assign(
:page_description,
"Get in touch with us for any questions or help with your order."
)
|> assign(:og_url, BerrypodWeb.Endpoint.url() <> "/contact")}
end
@impl true

View File

@@ -25,6 +25,7 @@ defmodule BerrypodWeb.Shop.Content do
%{
page_title: "About",
page_description: "Your story goes here this is sample content for the demo shop",
og_url: BerrypodWeb.Endpoint.url() <> "/about",
active_page: "about",
hero_title: "About the studio",
hero_description: "Your story goes here this is sample content for the demo shop",
@@ -39,6 +40,7 @@ defmodule BerrypodWeb.Shop.Content do
%{
page_title: "Delivery & returns",
page_description: "Everything you need to know about shipping and returns.",
og_url: BerrypodWeb.Endpoint.url() <> "/delivery",
active_page: "delivery",
hero_title: "Delivery & returns",
hero_description: "Everything you need to know about shipping and returns",
@@ -50,6 +52,7 @@ defmodule BerrypodWeb.Shop.Content do
%{
page_title: "Privacy policy",
page_description: "How we handle your personal information.",
og_url: BerrypodWeb.Endpoint.url() <> "/privacy",
active_page: "privacy",
hero_title: "Privacy policy",
hero_description: "How we handle your personal information",
@@ -61,6 +64,7 @@ defmodule BerrypodWeb.Shop.Content do
%{
page_title: "Terms of service",
page_description: "The terms and conditions governing purchases from our shop.",
og_url: BerrypodWeb.Endpoint.url() <> "/terms",
active_page: "terms",
hero_title: "Terms of service",
hero_description: "The legal bits",

View File

@@ -10,6 +10,7 @@ defmodule BerrypodWeb.Shop.Home do
socket =
socket
|> assign(:page_title, "Home")
|> assign(:og_url, BerrypodWeb.Endpoint.url() <> "/")
|> assign(:products, products)
{:ok, socket}

View File

@@ -26,6 +26,11 @@ defmodule BerrypodWeb.Router do
plug :put_secure_browser_headers
end
# Minimal pipeline for robots.txt and sitemap.xml
pipeline :seo do
plug :put_secure_browser_headers
end
pipeline :printify_webhook do
plug BerrypodWeb.Plugs.VerifyPrintifyWebhook
end
@@ -90,6 +95,14 @@ defmodule BerrypodWeb.Router do
get "/health", HealthController, :show
end
# SEO — crawlers need these without any session/auth overhead
scope "/", BerrypodWeb do
pipe_through [:seo]
get "/robots.txt", SeoController, :robots
get "/sitemap.xml", SeoController, :sitemap
end
# Cart API (session persistence for LiveView)
scope "/api", BerrypodWeb do
pipe_through [:browser]