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>
This commit is contained in:
nerandClaude Opus 5 committed 2026-08-14 10:41:09 +03:00
1 parent ecc57ff135
commit 01792a9669
10 files changed
+145 -34

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
type Unknown = Record<string, any>;
const identity = (payload: Unknown) => payload;
const named = ({ id, name, profile_path }: Unknown) => ({ id, name, profile_path });
const castMember = (member: Unknown) => ({
...named(member),
...('character' in member ? { character: member.character } : {}),
...(member.roles ? { roles: member.roles.map(({ character }: Unknown) => ({ character })) } : {}),
...(member.total_episode_count != null ? { total_episode_count: member.total_episode_count } : {})
});
const crewMember = (member: Unknown) => ({
...named(member),
...(member.job ? { job: member.job } : {}),
...(member.jobs ? { jobs: member.jobs.map(({ job }: Unknown) => ({ job })) } : {}),
...(member.total_episode_count != null ? { total_episode_count: member.total_episode_count } : {})
});
const withPhoto = ({ profile_path }: Unknown) => Boolean(profile_path);
const credits = ({ cast = [], crew = [], ...rest }: Unknown) => ({
...rest,
cast: cast.filter(withPhoto).map(castMember),
crew: crew.filter(withPhoto).map(crewMember)
});
const personCredit = (credit: Unknown) => ({
media_type: credit.media_type,
id: credit.id,
overview: credit.overview,
poster_path: credit.poster_path,
backdrop_path: credit.backdrop_path,
vote_average: credit.vote_average,
vote_count: credit.vote_count,
...(credit.media_type === 'tv'
? { first_air_date: credit.first_air_date, name: credit.name }
: { release_date: credit.release_date, title: credit.title })
});
const shown = ({ poster_path, media_type }: Unknown) => Boolean(poster_path) && ['movie', 'tv'].includes(media_type);
const person = ({ combined_credits, ...profile }: Unknown) => ({
id: profile.id,
name: profile.name,
biography: profile.biography,
birthday: profile.birthday,
deathday: profile.deathday,
place_of_birth: profile.place_of_birth,
known_for_department: profile.known_for_department,
profile_path: profile.profile_path,
...(combined_credits
? {
combined_credits: {
cast: (combined_credits.cast || []).filter(shown).map(personCredit),
crew: (combined_credits.crew || []).filter(shown).map(personCredit)
}
}
: {})
});
const trimmers: Array<[RegExp, (payload: Unknown) => Unknown]> = [
[/^(movie|tv)\/\d+\/(aggregate_)?credits$/, credits],
[/^person\/\d+$/, person]
];
const trim = (path: string, payload: unknown) => {
if (!payload || typeof payload !== 'object') return payload;
const [, trimmer = identity] = trimmers.find(([pattern]) => pattern.test(path)) || [];
return trimmer(payload as Unknown);
};
export default trim;