Skip to content

Commit 04f0973

Browse files
authored
fix: replace hardcoded localhost URL with BACKEND_URL env var in devcard loader (#218) (#257)
The server load function for /devcard/[id] was fetching card data from a hardcoded http://localhost:3000 URL, causing the route to fail silently in all non-local environments (staging, production, Docker). Replace the hardcoded URL with the BACKEND_URL environment variable, falling back to http://localhost:3000 for local development. This matches the existing pattern used by the /u/[username] route. Also improve error handling: wrap the fetch in a try/catch to handle network-level failures with a proper 500 response, distinguish 404 (card not found) from other backend errors, and re-throw SvelteKit HttpError objects so they are not swallowed by the catch block.
1 parent c122569 commit 04f0973

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import { error } from '@sveltejs/kit';
22
import type { PageServerLoad } from './$types';
33

4+
const API_BASE = process.env.BACKEND_URL || 'http://localhost:3000';
5+
46
export const load: PageServerLoad = async ({ params, fetch }) => {
57
const { id } = params;
68

7-
// Use internal fetch to reach the backend
8-
// In production, this would be the actual API URL
9-
const res = await fetch(`http://localhost:3000/api/u/card/${id}`);
9+
try {
10+
const res = await fetch(`${API_BASE}/api/u/card/${id}`);
1011

11-
if (!res.ok) {
12-
throw error(404, 'Card not found');
13-
}
12+
if (res.status === 404) {
13+
throw error(404, 'Card not found');
14+
}
1415

15-
const card = await res.json();
16-
return { card };
16+
if (!res.ok) {
17+
throw error(500, 'Failed to load card');
18+
}
19+
20+
const card = await res.json();
21+
return { card };
22+
} catch (err) {
23+
if (err && typeof err === 'object' && 'status' in err) {
24+
throw err;
25+
}
26+
throw error(500, 'Failed to connect to backend');
27+
}
1728
};

0 commit comments

Comments
 (0)