Files
nerandClaude Opus 5 01792a9669 perf(cache): edge-cache tmdb proxy, call tmdb directly on server, enable ISR
Three exceeded Vercel free tier limits (edge requests, function invocations,
fast origin transfer) all traced back to nothing being cached.

- Http defaults preventCache to false and the tmdb proxy now sends a real
  Cache-Control, so public responses are edge cached instead of missing on
  every request. Session scoped calls keep private, no-store.
- On the server movieAdvisor talks to tmdb directly. It used to fetch its own
  public /api/tmdb url, billing a second edge request, a second function
  invocation and the whole payload as origin transfer per server render.
- Add generateStaticParams to movie, tv and person pages. Without it Next
  treats the segment as fully dynamic and ignores revalidate, so the pages
  re-rendered on every request instead of using ISR.
- Trim credits and person payloads in the proxy: credits 72kb to 13kb,
  aggregate_credits 308kb to 102kb, person 191kb to 124kb. Only fields no
  mapper reads are dropped, mapped output is unchanged.
- Detail pages are cached now, so only a real tmdb 404 becomes notFound and
  other failures bubble up instead of being baked in as a 404 for a day.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-14 10:41:09 +03:00

99 lines
2.2 KiB
TypeScript

import { REVALIDATE } from '@/lib/cache';
type Search = Record<string, string | number | boolean | null | undefined>;
class Http {
constructor(
private base: string,
private authorization?: string
) {}
private async request<T, B = unknown>({
method,
url,
base = this.base,
search,
body,
preventCache = false
}: {
method: 'GET' | 'POST';
url: string;
base?: string;
search?: Search;
body?: B;
preventCache?: boolean;
}): Promise<T> {
const searchParams = new URLSearchParams(
Object.entries(search ?? {}).reduce((params, [key, value]) => {
if (value != null && value !== '') {
params.set(key, String(value));
}
return params;
}, new URLSearchParams())
);
const uncached = preventCache || method === 'POST';
if (uncached) {
searchParams.append('preventCache', '1');
}
const searchParamsString = searchParams.toString();
try {
const response = await fetch(`${base}${url}${searchParamsString ? `?${searchParamsString}` : ''}`, {
method,
headers: {
Authorization: this.authorization || '',
'Content-Type': 'application/json'
},
body: body ? JSON.stringify(body) : undefined,
next: uncached ? undefined : { revalidate: REVALIDATE }
});
if (!response.ok) {
return Promise.reject({
status: response.status,
statusText: response.statusText
});
}
return response.json();
} catch (error) {
return Promise.reject({ message: 'Network or parsing error', error });
}
}
get<T>(
url: string,
{ params, preventCache }: { params?: Search; preventCache?: boolean } = {},
base?: string
): Promise<T> {
return this.request<T>({
url,
method: 'GET',
search: params,
preventCache,
base
});
}
post<T, B = unknown>(
url: string,
{ params, preventCache }: { params?: Search; preventCache?: boolean } = {},
body?: B,
base?: string
): Promise<T> {
return this.request<T>({
url,
method: 'POST',
search: params,
body,
base,
preventCache
});
}
}
export default Http;