Run the SPA without a backend (local mode)

The app can now operate backend-agnostically: a production build still
asks on first start, but /setup also offers 'Continue without a backend'
(stored as the sentinel 'none'), and the shell adapts:

- Local mode shows only the e621-facing pages: Online (search, post view,
  favorites, blacklist editor, direct downloads) and Pools. Library,
  uploads, duplicates, stats, users, follows and similarity are hidden
  from the nav and palette and render a 'backend needed' state when
  reached directly; /detail/<e621 id> still works while /detail/J-x asks
  for a backend.
- e621 credentials are stored in this browser (j621.e621) and the
  Account page becomes a credentials-only screen; the store reads/writes
  locally instead of /api/auth/e621/.
- The header replaces the status pill and login/user area with an e621
  credentials button and a 'Setup Backend' button; the footer shows
  'Local mode — e621 features only' with the same entry point.
- In-library lookups (badges/browse markers) are skipped without a
  backend; 'Download to client' links straight to the e621 file instead
  of the backend proxy; follow buttons and palette follow toggles are
  hidden; api() fails fast with a clear message if something slips
  through.

Mode logic lives in lib/backend.ts (URL / '' same-origin / 'none') with
its matrix verified in Node; tsc, oxlint and the build are clean.
This commit is contained in:
2026-09-18 00:00:48 -05:00
parent 3bd5bc73dd
commit a904abdf20
19 changed files with 412 additions and 155 deletions
+6 -1
View File
@@ -1,5 +1,5 @@
import type { TempUpload } from "@/lib/types";
import { getBackendUrl } from "@/lib/backend";
import { getBackendUrl, hasBackend } from "@/lib/backend";
const TOKEN_KEY = "j621.token";
@@ -66,6 +66,11 @@ interface ApiOptions extends Omit<RequestInit, "body"> {
}
export async function api<T>(path: string, options: ApiOptions = {}): Promise<T> {
if (!hasBackend()) {
throw new ApiError(0, {
detail: "No backend is connected — set one up to use this feature.",
});
}
const { json, headers, ...rest } = options;
const finalHeaders = new Headers(headers);
let body = options.body;
+26 -12
View File
@@ -1,9 +1,12 @@
/**
* Where the J621 API lives.
* How this SPA reaches its backend, and whether it has one at all.
*
* A production build asks for this on first start (`/setup`) and remembers the
* answer per browser, so one build works same-origin AND against a backend on
* another domain. Same-origin is the empty string.
* Stored per browser in `localStorage["j621.backend"]`:
* - a URL → that origin serves the API
* - "" → same origin as this page
* - "none" → no backend: local/standalone mode (e621 features only)
* - absent → dev defaults to same-origin; a production build asks
* via /setup on first start.
*
* Self-hosters: set DEFAULT_BACKEND_URL to the origin where your API is
* reachable to prefill the setup screen — the equivalent of the matrix.org
@@ -11,26 +14,38 @@
*/
export const DEFAULT_BACKEND_URL = "";
/** Sentinel for "run without a backend" (local mode). */
export const NO_BACKEND = "none";
const STORAGE_KEY = "j621.backend";
/** Trim slashes, add a scheme when missing (http for localhost, else https). */
export function normalizeBackendUrl(value: string): string {
const trimmed = value.trim().replace(/\/+$/, "");
if (!trimmed) return "";
if (trimmed === NO_BACKEND) return NO_BACKEND;
if (/^https?:\/\//i.test(trimmed)) return trimmed;
const local = /^(localhost|127\.0\.0\.1|\[::1\])(?::|\/|$)/i.test(trimmed);
return `${local ? "http" : "https"}://${trimmed}`;
}
/** null = the user never chose; "" = same-origin. */
/** null = the user never chose; "" = same-origin; NO_BACKEND = local mode. */
export function getStoredBackendUrl(): string | null {
const raw = window.localStorage.getItem(STORAGE_KEY);
return raw === null ? null : normalizeBackendUrl(raw);
}
/** Current API origin; empty string means same-origin. */
/** True when an API is available (dev falls back to the same-origin proxy). */
export function hasBackend(): boolean {
const stored = getStoredBackendUrl();
if (stored === null) return !import.meta.env?.PROD;
return stored !== NO_BACKEND;
}
/** Current API origin; empty string means same-origin (or no backend). */
export function getBackendUrl(): string {
return getStoredBackendUrl() ?? DEFAULT_BACKEND_URL;
const stored = getStoredBackendUrl();
return stored === null || stored === NO_BACKEND ? "" : stored;
}
export function setBackendUrl(value: string): void {
@@ -38,11 +53,10 @@ export function setBackendUrl(value: string): void {
}
/**
* Production builds must be told where the API is on first start; the dev
* server keeps defaulting to the same-origin Vite proxy (visit /setup to
* change it manually).
* Production builds must be told where the API is on first start (they may
* pick "no backend" there). The dev server keeps defaulting to the
* same-origin Vite proxy; visit /setup to change it manually.
*/
export function needsBackendSetup(): boolean {
if (getStoredBackendUrl() !== null) return false;
return import.meta.env.PROD;
return getStoredBackendUrl() === null && Boolean(import.meta.env?.PROD);
}