mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 16:27:37 +01:00
scripts/site-template is now bitbaum/fleet templates/app — Next.js 16, better-auth accounts (password, link, passkey, 2FA, OrangeCat/bitbaum sign-in as env-gated OIDC), en/de/fr, nonce CSP, ai-kit/mail-kit/limitkit/bip-kit, tests and CI — copied at a pinned fleet commit by scripts/templates/sync-starter.sh. CI proves the copy is exact (sync-starter.sh --check), and starter-sync.yml opens the PR when fleet's starter moves. Never edited here. The kickoff's "nextjs-tailwind" starter seeds the same app from src/lib/starter-app.generated.ts (generated from the directory, pinned by scripts/test/starter-app.ts), minus the workflows register-site.sh adds with the site's CD. The repo is seeded in one Trees API call with inline contents instead of a blob POST per file (~85 for this starter). The widget token rides in .env.production; the starter's .gitleaks.toml allows NEXT_PUBLIC_* values. The new-from-scratch picker reads the light template list, so the starter's file bodies never reach a client bundle. Both paths give the site its database and auth/seal secrets ON THE BOX (scripts/hetzner/provision-db-on-box.sh: role + database, ~/.db-credentials, /opt/<slug>/shared/.env; existing secrets never replaced; nothing printed), and record the db in the register row so every deploy applies drizzle/. register-site.sh seeds the starter's full workflow set to starter repos and the golden CI floor to anything else, never overwriting another repo's ci.yml. test-site-template.sh follows the starter (verify SSOT, design tokens instead of a local palette) and its comment stripper is fixed: the sed range deleted the page from the doc comment to EOF, so every day-zero check had been passing against an empty string. check-template-contrast.mjs and the starter-lock generator are retired with the files they checked. Claude-Session: https://claude.ai/code/session_01W7K2Gm5wcbfCi7THtmV1Hy Co-authored-by: Claude <noreply@anthropic.com>
65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
import createMiddleware from "next-intl/middleware";
|
|
import type { NextRequest } from "next/server";
|
|
import { routing } from "./i18n/routing";
|
|
import { contentSecurityPolicy, newNonce } from "./lib/security";
|
|
|
|
const handleI18nRouting = createMiddleware(routing);
|
|
|
|
/**
|
|
* Every page request gets two things here:
|
|
*
|
|
* 1. A language. `/de/account` renders German; `/` redirects to the reader's.
|
|
* 2. A Content-Security-Policy with a fresh nonce, so the browser runs only
|
|
* scripts this response vouched for — Next's own, carrying the nonce.
|
|
*
|
|
* Next reads the nonce from the REQUEST's CSP header and stamps it on its own
|
|
* scripts, so the policy is forwarded on the request as well as set on the
|
|
* response. next-intl builds its own response, so the request headers are
|
|
* forwarded with Next's override mechanism rather than NextResponse.next().
|
|
*
|
|
* Nothing here reads, logs or stores anything about the person.
|
|
*/
|
|
export default function proxy(request: NextRequest) {
|
|
const nonce = newNonce();
|
|
const csp = contentSecurityPolicy(nonce, {
|
|
dev: process.env.NODE_ENV === "development",
|
|
widget: Boolean(process.env.NEXT_PUBLIC_FC_WIDGET_TOKEN),
|
|
});
|
|
|
|
const response = handleI18nRouting(request);
|
|
|
|
const overridden = response.headers.get("x-middleware-override-headers");
|
|
const names = new Set(overridden ? overridden.split(",") : []);
|
|
if (!overridden) {
|
|
// Next replaces the request's headers with exactly the overridden list,
|
|
// so the first override has to carry every original header along.
|
|
request.headers.forEach((value, key) => {
|
|
names.add(key);
|
|
response.headers.set(`x-middleware-request-${key}`, value);
|
|
});
|
|
}
|
|
for (const [key, value] of [
|
|
["content-security-policy", csp],
|
|
["x-nonce", nonce],
|
|
] as const) {
|
|
names.add(key);
|
|
response.headers.set(`x-middleware-request-${key}`, value);
|
|
}
|
|
response.headers.set("x-middleware-override-headers", [...names].join(","));
|
|
response.headers.set("Content-Security-Policy", csp);
|
|
return response;
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
{
|
|
source: "/((?!api|_next/static|_next/image|_vercel|.*\\..*).*)",
|
|
// A prefetch is not rendered for a reader and needs no nonce.
|
|
missing: [
|
|
{ type: "header", key: "next-router-prefetch" },
|
|
{ type: "header", key: "purpose", value: "prefetch" },
|
|
],
|
|
},
|
|
],
|
|
};
|