## Summary - Fixes periodic production crashes (undici ECONNREFUSED ::1) by bounding server cache size/lifetime and hardening server HTTP client. ### Root cause - React server cache (cache(...)) held axios responses indefinitely across many parameter combinations, causing slow memory growth until the Next.js app router worker was OOM-killed. The main server then failed IPC to the worker (ECONNREFUSED ::1:<port>). ### Changes - `app/lib/data.ts`: Replace unbounded cache(...) with unstable_cache and explicit keys; TTLs: 60s for teams/detail/favorites/user, 300s for meta (jobs/skills/accessories/raids/version). - `app/lib/api-utils.ts`: Add shared Axios instance with 15s timeout and keepAlive http/https agents; apply to GET/POST/PUT/DELETE helpers. - `pages/api/preview/[shortcode].ts`: Remove duplicate handler to dedupe route; retain the .tsx variant using `NEXT_PUBLIC_SIERO_API_URL`. ### Notes - Build currently has pre-existing app/pages route duplication errors; out of scope here but unrelated to this fix. - Ensure `NEXT_PUBLIC_SIERO_API_URL` and `NEXT_PUBLIC_SIERO_OAUTH_URL` are set on Railway. ### Risk/impact - Low risk; behavior is unchanged aside from bounded caching and resilient HTTP. - Cache TTLs can be tuned later if needed. ### Test plan - Verify saved/teams/user pages load and revalidate after TTL. - Validate API routes still proxy correctly; timeouts occur after ~15s for hung upstreams. - Monitor memory over several days; expect stable usage without steady growth.
198 lines
5.2 KiB
TypeScript
198 lines
5.2 KiB
TypeScript
import { unstable_cache } from 'next/cache';
|
|
import { fetchFromApi } from './api-utils';
|
|
|
|
// Cached server-side data fetching functions
|
|
// These are wrapped with React's cache function to deduplicate requests
|
|
|
|
// Get teams with optional filters
|
|
export async function getTeams({
|
|
element,
|
|
raid,
|
|
recency,
|
|
page = 1,
|
|
username,
|
|
}: {
|
|
element?: number;
|
|
raid?: string;
|
|
recency?: string;
|
|
page?: number;
|
|
username?: string;
|
|
}) {
|
|
const key = [
|
|
'getTeams',
|
|
String(element ?? ''),
|
|
String(raid ?? ''),
|
|
String(recency ?? ''),
|
|
String(page ?? 1),
|
|
String(username ?? ''),
|
|
];
|
|
|
|
const run = unstable_cache(async () => {
|
|
const queryParams: Record<string, string> = {};
|
|
if (element) queryParams.element = element.toString();
|
|
if (raid) queryParams.raid_id = raid;
|
|
if (recency) queryParams.recency = recency;
|
|
if (page) queryParams.page = page.toString();
|
|
|
|
let endpoint = '/parties';
|
|
if (username) {
|
|
endpoint = `/users/${username}/parties`;
|
|
}
|
|
|
|
const queryString = new URLSearchParams(queryParams).toString();
|
|
if (queryString) endpoint += `?${queryString}`;
|
|
|
|
try {
|
|
const data = await fetchFromApi(endpoint);
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to fetch teams', error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 60 });
|
|
|
|
return run();
|
|
}
|
|
|
|
// Get a single team by shortcode
|
|
export async function getTeam(shortcode: string) {
|
|
const key = ['getTeam', String(shortcode)];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const data = await fetchFromApi(`/parties/${shortcode}`);
|
|
return data;
|
|
} catch (error) {
|
|
console.error(`Failed to fetch team with shortcode ${shortcode}`, error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 60 });
|
|
return run();
|
|
}
|
|
|
|
// Get user info
|
|
export async function getUserInfo(username: string) {
|
|
const key = ['getUserInfo', String(username)];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const data = await fetchFromApi(`/users/info/${username}`);
|
|
return data;
|
|
} catch (error) {
|
|
console.error(`Failed to fetch user info for ${username}`, error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 60 });
|
|
return run();
|
|
}
|
|
|
|
// Get raid groups
|
|
export async function getRaidGroups() {
|
|
const key = ['getRaidGroups'];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const data = await fetchFromApi('/raids/groups');
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to fetch raid groups', error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 300 });
|
|
return run();
|
|
}
|
|
|
|
// Get version info
|
|
export async function getVersion() {
|
|
const key = ['getVersion'];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const data = await fetchFromApi('/version');
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to fetch version info', error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 300 });
|
|
return run();
|
|
}
|
|
|
|
// Get user's favorites/saved teams
|
|
export async function getFavorites() {
|
|
const key = ['getFavorites'];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const data = await fetchFromApi('/parties/favorites');
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to fetch favorites', error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 60 });
|
|
return run();
|
|
}
|
|
|
|
// Get all jobs
|
|
export async function getJobs(element?: number) {
|
|
const key = ['getJobs', String(element ?? '')];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const queryParams: Record<string, string> = {};
|
|
if (element) queryParams.element = element.toString();
|
|
|
|
let endpoint = '/jobs';
|
|
const queryString = new URLSearchParams(queryParams).toString();
|
|
if (queryString) endpoint += `?${queryString}`;
|
|
|
|
const data = await fetchFromApi(endpoint);
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to fetch jobs', error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 300 });
|
|
return run();
|
|
}
|
|
|
|
// Get job by ID
|
|
export async function getJob(jobId: string) {
|
|
const key = ['getJob', String(jobId)];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const data = await fetchFromApi(`/jobs/${jobId}`);
|
|
return data;
|
|
} catch (error) {
|
|
console.error(`Failed to fetch job with ID ${jobId}`, error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 300 });
|
|
return run();
|
|
}
|
|
|
|
// Get job skills
|
|
export async function getJobSkills(jobId?: string) {
|
|
const key = ['getJobSkills', String(jobId ?? '')];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const endpoint = jobId ? `/jobs/${jobId}/skills` : '/jobs/skills';
|
|
const data = await fetchFromApi(endpoint);
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to fetch job skills', error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 300 });
|
|
return run();
|
|
}
|
|
|
|
// Get job accessories
|
|
export async function getJobAccessories(jobId: string) {
|
|
const key = ['getJobAccessories', String(jobId)];
|
|
const run = unstable_cache(async () => {
|
|
try {
|
|
const data = await fetchFromApi(`/jobs/${jobId}/accessories`);
|
|
return data;
|
|
} catch (error) {
|
|
console.error(`Failed to fetch accessories for job ${jobId}`, error);
|
|
throw error;
|
|
}
|
|
}, key, { revalidate: 300 });
|
|
return run();
|
|
}
|