diff --git a/.env.example b/.env.example index 5fdd8a8..1c7ea11 100644 --- a/.env.example +++ b/.env.example @@ -32,10 +32,11 @@ SITE_TIMEZONE=Europe/Oslo # Public URLs the browser uses: ://admin.. # They must match the address bar exactly (SvelteKit's form CSRF check). -# Local default: http + :8080. Behind a reverse proxy / TLS on 443, e.g.: +# Local default: http + :8080. Behind a reverse proxy / TLS on 443: # URL_SCHEME=https -# URL_PORT= -# (empty URL_PORT = no port in the URL; leave the line out to get :8080) +# URL_PORT=:443 +# (:443 is the https default, so URLs come out without a port. An empty +# value works too; a MISSING URL_PORT means :8080.) DOMAIN=localhost URL_SCHEME=http URL_PORT=:8080 diff --git a/AGENTS.md b/AGENTS.md index d9281ce..e2168fd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -173,7 +173,7 @@ cd app-public && npm run build ## Environment - `DOMAIN` — base domain for subdomain routing. Defaults to `localhost` for local dev. Set in `docker-compose.yml` or `.env`. -- `URL_SCHEME` (default `http`), `URL_PORT` (default `:8080`; set it empty for no port) — build every browser-facing URL below as `://.`. They must match the address bar exactly: a mismatched `ORIGIN` makes SvelteKit reject form POSTs (admin login) with "Cross-site POST form submissions are forbidden". Behind a TLS reverse proxy: `URL_SCHEME=https`, `URL_PORT=`. (podman-compose 1.5 can't do nested `${A:-${B}}` defaults, hence two flat variables.) +- `URL_SCHEME` (default `http`), `URL_PORT` (default `:8080` when unset; `:443` or empty for no port) — build every browser-facing URL below as `://.`. They must match the address bar exactly: a mismatched `ORIGIN` makes SvelteKit reject form POSTs (admin login) with "Cross-site POST form submissions are forbidden". Behind a TLS reverse proxy: `URL_SCHEME=https`, `URL_PORT=:443` (normalised away by `new URL().origin`; deploy panels often can't store an empty value, and a missing one means `:8080`). (podman-compose 1.5 can't do nested `${A:-${B}}` defaults, hence two flat variables.) - `PUBLIC_POCKETBASE_URL` — public-facing PocketBase URL (`://db.${DOMAIN}`). Set in `docker-compose.yml`. - `POCKETBASE_INTERNAL_URL` — internal Docker network URL (`http://pocketbase:80`). Set in `docker-compose.yml`. - `ORIGIN` — SvelteKit origin for SSR. Differs per app: `://admin.${DOMAIN}` (admin), `://www.${DOMAIN}` (public). diff --git a/README.md b/README.md index 60e87fe..05dcb3b 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Caddy's `.localhost` handling works without DNS or hosts-file changes. Source ch ```sh DOMAIN=example.com # → admin.example.com, www.example.com, db.example.com URL_SCHEME=https -URL_PORT= # empty: no port in the URL +URL_PORT=:443 # default https port → no port in the URLs (don't leave it out: missing = :8080) CADDY_SCHEME=http # this stack's Caddy stays plain HTTP behind the TLS proxy ``` diff --git a/app-admin/src/routes/api/import/+server.ts b/app-admin/src/routes/api/import/+server.ts index f50609a..f9314c9 100644 --- a/app-admin/src/routes/api/import/+server.ts +++ b/app-admin/src/routes/api/import/+server.ts @@ -1,6 +1,6 @@ -import { env } from '$env/dynamic/public'; import { error, json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; +import { pbUrl } from '$lib/pb'; import { dataUriToFile, restorePlaceholders, @@ -41,7 +41,7 @@ export const POST: RequestHandler = async ({ request, locals }) => { formData.append('alt', filename.replace(/\.[^/.]+$/, '').replace(/[-_]/g, ' ')); const record = await locals.pb.collection('media').create(formData); - const publicUrl = env.PUBLIC_POCKETBASE_URL; + const publicUrl = pbUrl; const newUrl = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`; keyToUrl.set(key, newUrl); diff --git a/app-admin/src/routes/api/upload/+server.ts b/app-admin/src/routes/api/upload/+server.ts index e5571d3..348cd0e 100644 --- a/app-admin/src/routes/api/upload/+server.ts +++ b/app-admin/src/routes/api/upload/+server.ts @@ -1,6 +1,6 @@ -import { env } from '$env/dynamic/public'; import { error, json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; +import { pbUrl } from '$lib/pb'; export const POST: RequestHandler = async ({ request, locals }) => { const formData = await request.formData(); @@ -17,7 +17,7 @@ export const POST: RequestHandler = async ({ request, locals }) => { try { const record = await locals.pb.collection('media').create(uploadForm); - const publicUrl = env.PUBLIC_POCKETBASE_URL; + const publicUrl = pbUrl; const url = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`; return json({ record, url }); } catch (e) { diff --git a/app-admin/src/routes/pages/+page.svelte b/app-admin/src/routes/pages/+page.svelte index 3e3cf6a..b31b074 100644 --- a/app-admin/src/routes/pages/+page.svelte +++ b/app-admin/src/routes/pages/+page.svelte @@ -2,11 +2,12 @@ import { env } from '$env/dynamic/public'; import { confirmSubmit } from '$lib/admin/confirm'; import Modal from '$lib/admin/Modal.svelte'; + import { baseUrl } from '$shared/url'; let { data, form } = $props(); let showCreate = $state(false); - const siteUrl = (env.PUBLIC_SITE_URL || '').replace(/\/$/, ''); + const siteUrl = baseUrl(env.PUBLIC_SITE_URL); const publicUrl = (slug: string) => `${siteUrl}/${slug === 'home' ? '' : slug}`; diff --git a/docker-compose.yml b/docker-compose.yml index 881482d..e061218 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -36,7 +36,9 @@ services: POCKETBASE_INTERNAL_URL: http://pocketbase:80 # Browser-facing URLs. They must match the address bar exactly, or # SvelteKit rejects logins/forms ("Cross-site POST form submissions are - # forbidden"). Behind a TLS proxy: URL_SCHEME=https and URL_PORT= (empty). + # forbidden"). Behind a TLS proxy: URL_SCHEME=https and URL_PORT=:443 + # (the default port is dropped, giving https://admin.). If + # URL_PORT is not set at all it defaults to :8080 (local dev). PUBLIC_POCKETBASE_URL: ${URL_SCHEME:-http}://db.${DOMAIN:-localhost}${URL_PORT-:8080} ORIGIN: ${URL_SCHEME:-http}://admin.${DOMAIN:-localhost}${URL_PORT-:8080} PUBLIC_SITE_URL: ${URL_SCHEME:-http}://www.${DOMAIN:-localhost}${URL_PORT-:8080} diff --git a/packages/shared/src/pb.ts b/packages/shared/src/pb.ts index 0797bb5..a5d710e 100644 --- a/packages/shared/src/pb.ts +++ b/packages/shared/src/pb.ts @@ -1,8 +1,9 @@ import PocketBase from 'pocketbase'; import { env } from '$env/dynamic/public'; import { browser } from '$app/environment'; +import { baseUrl } from './url'; -export const pbUrl = env.PUBLIC_POCKETBASE_URL; +export const pbUrl = baseUrl(env.PUBLIC_POCKETBASE_URL); export function createPocketBase(authCookie?: string, url = pbUrl) { const pb = new PocketBase(url); diff --git a/packages/shared/src/server/email/index.ts b/packages/shared/src/server/email/index.ts index 9b775e0..4b2ecd6 100644 --- a/packages/shared/src/server/email/index.ts +++ b/packages/shared/src/server/email/index.ts @@ -13,6 +13,7 @@ import { import { normalizeEmailTemplate } from '../../email/defaults'; import { renderEmail, sampleEmailContext, type EmailContext, type EmailLine } from '../../email/render'; import { buildIcs } from './ics'; +import { baseUrl } from '../../url'; import { sendEmail, type SendResult } from './transport'; export { sendEmail, isEmailConfigured, emailSender, type SendResult, type OutgoingEmail } from './transport'; @@ -58,7 +59,7 @@ async function getGlobalData(pb: PocketBase, key: string, fall /** Public site URL — the admin app's ORIGIN is the admin subdomain, so prefer PUBLIC_SITE_URL. */ export function emailSiteUrl(): string { - return (publicEnv.PUBLIC_SITE_URL || env.ORIGIN || '').replace(/\/$/, ''); + return baseUrl(publicEnv.PUBLIC_SITE_URL || env.ORIGIN); } export async function loadEmailSettings(pb: PocketBase): Promise { diff --git a/packages/shared/src/url.ts b/packages/shared/src/url.ts new file mode 100644 index 0000000..da51374 --- /dev/null +++ b/packages/shared/src/url.ts @@ -0,0 +1,15 @@ +/** + * A configured base URL without a trailing slash or a default port, e.g. + * "https://db.example.com:443/" → "https://db.example.com". docker-compose + * builds these from URL_SCHEME/URL_PORT, and URL_PORT=:443 is the usual + * production value; the URLs end up in saved page data and emails. + */ +export function baseUrl(value: string | undefined | null): string { + const raw = String(value ?? '').trim(); + try { + const url = new URL(raw); + return (url.origin + url.pathname).replace(/\/+$/, ''); + } catch { + return raw.replace(/\/+$/, ''); + } +}