Files
movie-advisor/app/(site)/(show)/showPage.tsx
T
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

105 lines
3.1 KiB
TypeScript

import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import { Metadata } from 'next';
import { notFound, permanentRedirect } from 'next/navigation';
import JsonLd from '@/components/JsonLd';
import detailsQuery from '@/data/queries/details';
import { SITE_URL, TITLE } from '@/env';
import getQueryClient from '@/lib/queryClient';
import { isMissing, showPath } from '@/lib/utils';
import Container from './container';
interface Props {
params: { id: string };
}
export const createShowMetadata =
(showType: Show['type']) =>
async ({ params: { id } }: Props): Promise<Metadata> => {
const queryClient = getQueryClient();
const showId = parseInt(id, 10);
if (!showId) return notFound();
try {
const { title, overview, poster, backdrop } = await queryClient.fetchQuery(detailsQuery({ showId, showType }));
const path = showPath({ type: showType, id: showId, title });
return {
title: `${title} | ${TITLE}`,
description: overview,
alternates: { canonical: path },
openGraph: {
title,
description: overview,
siteName: TITLE,
images: [backdrop || poster['2x']],
type: showType === 'movie' ? 'video.movie' : 'video.tv_show',
url: path
}
};
} catch (error) {
if (isMissing(error)) return notFound();
throw error;
}
};
export const createShowPage = (showType: Show['type']) => {
const ShowPage = async ({ params: { id } }: Props) => {
const queryClient = getQueryClient();
const showId = parseInt(id, 10);
if (!showId) return notFound();
const details = await queryClient.fetchQuery(detailsQuery({ showId, showType })).catch((error) => {
if (isMissing(error)) return null;
throw error;
});
if (!details) return notFound();
const path = showPath({ type: showType, id: showId, title: details.title });
if (decodeURIComponent(`/${showType}/${id}`) !== path) permanentRedirect(path);
const jsonLd = {
'@context': 'https://schema.org',
'@type': showType === 'movie' ? 'Movie' : 'TVSeries',
name: details.title,
description: details.overview || undefined,
image: details.poster['2x'] || undefined,
datePublished: details.release || undefined,
genre: details.genres?.map(({ name }) => name),
url: `${SITE_URL}${path}`,
...(showType === 'movie' && details.runtime ? { duration: `PT${details.runtime}M` } : {}),
...(details.imdb_id ? { sameAs: `https://www.imdb.com/title/${details.imdb_id}/` } : {}),
...(details.votes
? {
aggregateRating: {
'@type': 'AggregateRating',
ratingValue: Math.round(details.rating * 10) / 10,
ratingCount: details.votes,
bestRating: 10,
worstRating: 0
}
}
: {})
};
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<JsonLd data={jsonLd} />
<Container showId={showId} showType={showType} />
</HydrationBoundary>
);
};
return ShowPage;
};