From 6a441675b9e9ae6ad379a438c081e6b5496fd956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ullrich=20Sch=C3=A4fer?= Date: Sat, 23 May 2026 18:55:18 +0200 Subject: [PATCH] Fix import action crashing in dev when pg-boss is not initialized MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The action was doing an internal self-fetch to /api/sync/komoot/import, which returned plain text on error (boss not initialized) — the JSON.parse then threw "Unexpected token U". Fix: inline the action logic directly in the route and fall back to a fire-and-forget runKomootBulkImport call when pg-boss is unavailable (i.e. dev mode with Vite, not the custom server). Co-Authored-By: Claude Sonnet 4.6 --- .../journal/app/routes/sync.import.komoot.tsx | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/apps/journal/app/routes/sync.import.komoot.tsx b/apps/journal/app/routes/sync.import.komoot.tsx index b8b51f7..2512942 100644 --- a/apps/journal/app/routes/sync.import.komoot.tsx +++ b/apps/journal/app/routes/sync.import.komoot.tsx @@ -50,14 +50,41 @@ export async function action({ request }: Route.ActionArgs) { const user = await getSessionUser(request); if (!user) return redirect("/auth/login"); - const resp = await fetch( - new URL("/api/sync/komoot/import", new URL(request.url).origin), - { method: "POST", headers: { cookie: request.headers.get("cookie") ?? "" } }, - ); - if (!resp.ok) { - const body = (await resp.json()) as { error?: string }; - return data({ error: body.error ?? "failed" }, { status: resp.status }); + const { randomUUID } = await import("node:crypto"); + const { getDb } = await import("~/lib/db"); + const { importBatches } = await import("@trails-cool/db/schema/journal"); + + const service = await getService(user.id, "komoot"); + if (!service) return redirect("/settings/connections/komoot"); + + const db = getDb(); + const batchId = randomUUID(); + await db.insert(importBatches).values({ + id: batchId, + userId: user.id, + connectionId: service.id, + provider: "komoot", + status: "pending", + }); + + // Try to enqueue via pg-boss (available in production / custom server). + // In dev (Vite), boss is never initialized — fall back to running inline + // in a detached promise so the response is still immediate. + try { + const { getBoss } = await import("~/lib/boss.server"); + const boss = getBoss(); + await boss.send("komoot-bulk-import", { + batchId, + userId: user.id, + creds: service.credentials, + }); + } catch { + const { runKomootBulkImport } = await import("~/lib/komoot-bulk-import.server"); + const creds = service.credentials as Parameters[2]; + // Fire-and-forget — don't await so the redirect happens immediately + void runKomootBulkImport(batchId, user.id, creds).catch(() => {}); } + return redirect("/sync/import/komoot"); }