Transactional mail, crawler metadata, and asset measurement (#179)

* Send transactional email from no-reply@mail.innersquare.app

Centralise the sender identity, reply-to address and SES configuration set in Chesstrainer.Mail so no notifier can drift. Add MAIL_REDIRECT_TO for smoke tests and a mix mail.check task that exercises the SES mailbox simulators, and document the setup in the deploy guide and .env.example.

* Tighten crawler metadata and keep play pages out of search

Canonical URLs no longer carry query strings, public pages get an OG image, WebSite JSON-LD and a per-page language, and the sitemap dates the landing page only. Play and guest-training routes are marked noindex so tens of thousands of near-identical pages stay out of the index.

* Measure built asset sizes with mix assets.sizes

Report raw, gzip and brotli sizes for built JS and CSS, and record the current numbers plus the board-code split recommendation in docs/assets.md.
This commit is contained in:
2026-11-17 10:29:45 +01:00
committed by GitHub
parent b33c5e334a
commit 37e0ac8e56
24 changed files with 666 additions and 44 deletions
+101
View File
@@ -0,0 +1,101 @@
defmodule Mix.Tasks.Assets.Sizes do
use Mix.Task
@shortdoc "Report raw, gzip and brotli sizes of built assets"
@moduledoc """
Prints the size of every built JavaScript and CSS file under
`priv/static/assets`, uncompressed and compressed, so bundle changes can be
measured rather than guessed.
mix assets.sizes
Run it after `mix assets.build` (development, unminified) or
`mix assets.deploy` (production, minified and digested). Production numbers
are the ones that matter: they are what the browser downloads.
Brotli sizes are shown as a dash when the `brotli` CLI is not installed.
"""
@assets_globs ["priv/static/assets/js/**/*.js", "priv/static/assets/css/**/*.css"]
@impl true
def run(_argv) do
files = @assets_globs |> Enum.flat_map(&Path.wildcard/1) |> Enum.sort()
if files == [] do
Mix.raise("no built assets found under priv/static/assets; run mix assets.build")
end
rows = Enum.map(files, &measure/1)
print(rows)
end
defp measure(path) do
contents = File.read!(path)
%{
path: Path.relative_to_cwd(path),
raw: byte_size(contents),
gzip: byte_size(:zlib.gzip(contents)),
brotli: brotli_size(path)
}
end
defp brotli_size(path) do
with executable when is_binary(executable) <- System.find_executable("brotli"),
{output, 0} <- System.cmd(executable, ["-q", "11", "-c", path], into: "") do
byte_size(output)
else
_unavailable -> nil
end
end
defp print(rows) do
widths = column_widths(rows)
header = [
pad("file", widths.path),
pad("raw", widths.raw, :right),
pad("gzip", widths.gzip, :right),
pad("brotli", widths.brotli, :right)
]
Mix.shell().info(Enum.join(header, " "))
Mix.shell().info(String.duplicate("-", Enum.sum(Map.values(widths)) + 6))
Enum.each(rows, &print_row(&1, widths))
end
defp print_row(row, widths) do
values = [
pad(row.path, widths.path),
pad(human(row.raw), widths.raw, :right),
pad(human(row.gzip), widths.gzip, :right),
pad(human(row.brotli), widths.brotli, :right)
]
Mix.shell().info(Enum.join(values, " "))
end
defp column_widths(rows) do
%{
path: rows |> Enum.map(&String.length(&1.path)) |> Enum.max(),
raw: rows |> Enum.map(&String.length(human(&1.raw))) |> Enum.max(),
gzip: rows |> Enum.map(&String.length(human(&1.gzip))) |> Enum.max(),
brotli: rows |> Enum.map(&String.length(human(&1.brotli))) |> Enum.max()
}
end
defp pad(value, width, align \\ :left) do
value = to_string(value)
case align do
:right -> String.pad_leading(value, width)
:left -> String.pad_trailing(value, width)
end
end
defp human(nil), do: "-"
defp human(bytes) when bytes < 1024, do: "#{bytes} B"
defp human(bytes), do: "#{Float.round(bytes / 1024, 1)} KB"
end
+84
View File
@@ -0,0 +1,84 @@
defmodule Mix.Tasks.Mail.Check do
use Mix.Task
@shortdoc "Send test email through the configured mailer and SES simulators"
@moduledoc """
Sends a test message through the application mailer, applying the same house
rules as real mail: From identity, Reply-To, and the SES configuration set.
mix mail.check --to you@example.com
By default it also sends to the SES mailbox simulator addresses so bounce and
complaint wiring can be exercised without touching a real inbox:
* `success@simulator.amazonses.com`
* `bounce@simulator.amazonses.com`
* `complaint@simulator.amazonses.com`
The simulators only do something when the mailer is the SES adapter. Pass
`--no-simulators` to send only to the given address.
If `MAIL_REDIRECT_TO` is set, every recipient is rewritten to that address
first (including the simulators), so this task cannot reach users even in
production.
"""
alias Chesstrainer.Mail
@simulators [
"success@simulator.amazonses.com",
"bounce@simulator.amazonses.com",
"complaint@simulator.amazonses.com"
]
@impl true
def run(argv) do
{opts, _args} = OptionParser.parse!(argv, strict: [to: :string, simulators: :boolean])
recipient =
opts[:to] ||
Mix.raise("pass the address to check: mix mail.check --to you@example.com")
Mix.Task.run("app.start")
targets = if opts[:simulators] == false, do: [recipient], else: [recipient | @simulators]
Mix.shell().info("adapter: #{inspect(adapter())}")
if redirect(), do: Mix.shell().info("redirect: every message goes to #{redirect()}")
Enum.each(targets, &send_message/1)
end
defp send_message(recipient) do
email =
Swoosh.Email.new()
|> Swoosh.Email.to(recipient)
|> Swoosh.Email.subject("Mail check #{DateTime.utc_now() |> DateTime.to_iso8601()}")
|> Swoosh.Email.text_body(
"This is a delivery check from the chess trainer. " <>
"If you can read it, the mailer, sender identity and configuration set are wired up."
)
|> Mail.prepare()
case Chesstrainer.Mailer.deliver(email) do
{:ok, metadata} ->
Mix.shell().info("sent #{recipient} #{format_metadata(metadata)}")
{:error, reason} ->
Mix.shell().error("failed #{recipient} #{inspect(reason)}")
end
end
defp format_metadata(%{id: id}) when is_binary(id), do: "(message id #{id})"
defp format_metadata(_metadata), do: ""
defp adapter do
:chesstrainer
|> Application.get_env(Chesstrainer.Mailer, [])
|> Keyword.get(:adapter)
end
defp redirect, do: Application.get_env(:chesstrainer, :mail_redirect_to)
end