Files
familienarchiv/frontend/src/routes/api/[...path]/+server.ts
Marcel c47c802ef5
Some checks failed
CI / Unit & Component Tests (push) Failing after 2m41s
CI / OCR Service Tests (push) Successful in 32s
CI / Backend Unit Tests (push) Failing after 2m49s
CI / Unit & Component Tests (pull_request) Failing after 2m43s
CI / OCR Service Tests (pull_request) Successful in 35s
CI / Backend Unit Tests (pull_request) Failing after 2m46s
fix(transcription): replace sendBeacon with fetch keepalive; add catch-all API proxy
sendBeacon always sends POST, but the backend expects PUT for block updates, so
saves were silently dropped on page unload.  Replace with fetch({ keepalive: true,
method: 'PUT' }) which survives navigation and uses the correct HTTP method.

Add a catch-all SvelteKit server route at /api/[...path] so all client-side API
calls work in production (without the Vite dev proxy).  More-specific routes
(/api/persons, /api/tags, /api/documents/[id]/file) keep precedence.

Closes #204

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 15:56:53 +02:00

34 lines
1.3 KiB
TypeScript

import type { RequestHandler } from './$types';
import { env } from 'process';
const NO_BODY_METHODS = new Set(['GET', 'HEAD', 'DELETE']);
async function proxy(event: Parameters<RequestHandler>[0]): Promise<Response> {
const apiUrl = env.API_INTERNAL_URL || 'http://localhost:8080';
const backendUrl = `${apiUrl}/api/${event.params.path}${event.url.search}`;
const contentType = event.request.headers.get('Content-Type');
const hasBody = !NO_BODY_METHODS.has(event.request.method);
const response = await event.fetch(backendUrl, {
method: event.request.method,
headers: contentType ? { 'Content-Type': contentType } : {},
body: hasBody ? await event.request.arrayBuffer() : undefined
});
const responseHeaders: Record<string, string> = {};
const responseContentType = response.headers.get('Content-Type');
if (responseContentType) responseHeaders['Content-Type'] = responseContentType;
return new Response(response.body, {
status: response.status,
headers: responseHeaders
});
}
export const GET: RequestHandler = (event) => proxy(event);
export const POST: RequestHandler = (event) => proxy(event);
export const PUT: RequestHandler = (event) => proxy(event);
export const PATCH: RequestHandler = (event) => proxy(event);
export const DELETE: RequestHandler = (event) => proxy(event);