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>
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
import { MOVIE_DB_API_URL, MOVIE_DB_TOKEN } from '@/env';
|
|
import { PRIVATE_CACHE_CONTROL, PUBLIC_CACHE_CONTROL, REVALIDATE } from '@/lib/cache';
|
|
|
|
import trim from './trim';
|
|
|
|
type TMDBParams = {
|
|
params: {
|
|
path: string[];
|
|
};
|
|
};
|
|
|
|
const upstream = (path: string[], searchParams: URLSearchParams) => {
|
|
const params = new URLSearchParams(searchParams);
|
|
|
|
params.delete('preventCache');
|
|
|
|
const query = params.toString();
|
|
|
|
return `${MOVIE_DB_API_URL}/${path.join('/')}${query ? `?${query}` : ''}`;
|
|
};
|
|
|
|
const headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${MOVIE_DB_TOKEN}` };
|
|
|
|
export async function GET({ nextUrl: { searchParams } }: NextRequest, { params: { path } }: TMDBParams) {
|
|
const isPrivate = Boolean(searchParams.get('preventCache') || searchParams.get('session_id'));
|
|
|
|
try {
|
|
const response = await fetch(upstream(path, searchParams), {
|
|
headers,
|
|
next: isPrivate ? undefined : { revalidate: REVALIDATE }
|
|
});
|
|
|
|
if (!response.ok) {
|
|
return NextResponse.json({ error: 'Failed to fetch data from TMDb' }, { status: response.status });
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
return NextResponse.json(trim(path.join('/'), data), {
|
|
headers: { 'Cache-Control': isPrivate ? PRIVATE_CACHE_CONTROL : PUBLIC_CACHE_CONTROL }
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json({ error: 'An error occurred' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest, { params: { path } }: TMDBParams) {
|
|
try {
|
|
const requestBody = await request.json();
|
|
|
|
const response = await fetch(upstream(path, request.nextUrl.searchParams), {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify(requestBody)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
return NextResponse.json({ error: 'Failed to post data to TMDb' }, { status: response.status });
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
return NextResponse.json(data, { headers: { 'Cache-Control': PRIVATE_CACHE_CONTROL } });
|
|
} catch (error) {
|
|
return NextResponse.json({ error: 'An error occurred' }, { status: 500 });
|
|
}
|
|
}
|