Files
heidi/next.config.mjs

93 lines
4.1 KiB
JavaScript

import { execSync } from "node:child_process";
import { WIDTHS, mediaBase } from "./lib/media/variants.mjs";
/** Short commit SHA baked in at build time so /api/health can say which build is live. */
function commitSha() {
try {
return execSync("git rev-parse --short HEAD", { stdio: ["ignore", "pipe", "ignore"] }).toString().trim();
} catch {
return "unknown";
}
}
/** Same precedence as lib/config/site.ts: SITE_URL, AUTH_URL, NEXT_PUBLIC_SITE_URL, else the old host. */
function siteUrl(values = process.env) {
const raw = [values.SITE_URL, values.AUTH_URL, values.NEXT_PUBLIC_SITE_URL].find((v) => v && v.trim());
try {
const url = new URL(raw.trim().replace(/\/+$/, ""));
if (url.protocol === "https:" && url.pathname === "/" && !url.search && !url.hash) return url.origin;
} catch {
// default below
}
return "https://heidi.orangecat.ch";
}
/**
* How photographs are served (`lib/media/variants.mjs`). With
* `NEXT_PUBLIC_MEDIA_BASE` set, `next/image` uses the custom loader, which
* spells the bucket's address of a variant rendered at build time
* (`lib/media/manifest.json`): the box does no image work, and its
* `/_next/image` answers 404 — which is why the loader is on ONLY with a
* base, never as a fallback. Unset, it is `next/image`'s own optimiser over
* the files in `public/photos`, as before. A `NEXT_PUBLIC_` variable is read
* when the site is built; on the box `scripts/media/ensure.mjs` sets it
* (in `.env.production.local`) once the bucket is known to serve every variant.
*/
export function imagesConfig(base = process.env.NEXT_PUBLIC_MEDIA_BASE) {
const shared = {
// 75 is the default; 85 is what a scene photograph is asked for at
// (`SCENE_QUALITY` in shared/scene.tsx). Next 16 refuses any quality
// not listed here. AVIF first: about half the bytes of WebP.
qualities: [75, 85],
formats: ["image/avif", "image/webp"],
};
// The widths the variants are rendered at, so each srcset entry names the width its file has.
return mediaBase(base) ? { ...shared, loader: "custom", loaderFile: "./lib/media/loader.mjs", deviceSizes: [...WIDTHS] } : shared;
}
/** @type {import('next').NextConfig} */
/** @type {import('next').NextConfig['redirects']} */
async function redirects() {
return [
{
// `/:locale/check` was a real, indexed page until the checker became
// evidence on the method page. Links to it exist in the wild and in
// Google's index, so it moves permanently rather than 404s — a retired
// URL that answers 404 throws away whatever standing it had.
// The locale is spelled out rather than left as `:locale`. A bare
// wildcard matches ANY first segment — including `api`, so `/api/check`
// 308'd to `/api/method` and the checker silently stopped working while
// the page around it still rendered perfectly. `lib/i18n/routes.test.ts`
// asserts this list equals LOCALES so the two cannot drift.
// Research merged into Method — the evidence is the argument FOR the
// method rather than a peer of it. It was indexed and cited, so it moves
// permanently and lands on the evidence section rather than the top.
source: "/:locale(de|gsw|fr|it|rm|en|ru)/research",
destination: "/:locale/method#facts",
permanent: true,
},
{
source: "/:locale(de|gsw|fr|it|rm|en|ru)/check",
destination: "/:locale/method",
permanent: true,
},
];
}
const nextConfig = {
redirects,
// The box's launch.sh looks for a server.js — see sync-infra.sh.
output: "standalone",
reactStrictMode: true,
// A URL that matches no route at all (a typo, a removed page, `/xx/…`) used
// to get Next's bare English «404 This page could not be found.» with no
// header and no way home, because the root layout lives under `[locale]`.
// See `app/global-not-found.tsx`.
experimental: { globalNotFound: true },
images: imagesConfig(),
// The site address is read from the env by lib/config/site.ts on the server;
// the browser build only sees NEXT_PUBLIC_*, so the resolved value goes there.
env: { COMMIT_SHA: commitSha(), NEXT_PUBLIC_SITE_URL: siteUrl() },
};
export default nextConfig;