feat(seo): slugged show urls, sr-only homepage intro, ISR detail pages
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ce630cd029
commit
735c63471a
11 files changed
+103
-28
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
|
||||
import { Metadata } from 'next';
|
||||
import Link from 'next/link';
|
||||
|
||||
import JsonLd from '@/components/JsonLd';
|
||||
import randomQuery, { generatePage } from '@/data/queries/random';
|
||||
@@ -24,6 +25,22 @@ const Random = async () => {
|
||||
<JsonLd data={{ '@context': 'https://schema.org', '@type': 'WebSite', name: TITLE, url: SITE_URL }} />
|
||||
<h1 className='sr-only'>{TITLE} — Discover Your Next Favorite Movie</h1>
|
||||
<Container page={page} />
|
||||
<section className='sr-only'>
|
||||
<h2>Discover your next favorite movie</h2>
|
||||
<p>
|
||||
{TITLE} helps you decide what to watch: spin through random movie recommendations right on this page, browse
|
||||
the{' '}
|
||||
<Link className='underline underline-offset-2' href='/popular'>
|
||||
most popular movies
|
||||
</Link>{' '}
|
||||
everyone is talking about, or dive into the{' '}
|
||||
<Link className='underline underline-offset-2' href='/top'>
|
||||
top-rated films
|
||||
</Link>{' '}
|
||||
of all time. Open any title to see its rating, overview, cast, and similar shows — and keep favorites and a
|
||||
watchlist so you never lose track of what to watch next.
|
||||
</p>
|
||||
</section>
|
||||
</HydrationBoundary>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { createShowMetadata, createShowPage } from '../../showPage';
|
||||
|
||||
export const revalidate = 86400;
|
||||
|
||||
export const generateMetadata = createShowMetadata('movie');
|
||||
|
||||
const MoviePage = createShowPage('movie');
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
|
||||
import { Metadata } from 'next';
|
||||
import { notFound } from 'next/navigation';
|
||||
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 { showPath } from '@/lib/utils';
|
||||
|
||||
import Container from './container';
|
||||
|
||||
@@ -18,22 +19,26 @@ export const createShowMetadata =
|
||||
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: Number(id), showType })
|
||||
);
|
||||
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: `/${showType}/${id}` },
|
||||
alternates: { canonical: path },
|
||||
openGraph: {
|
||||
title,
|
||||
description: overview,
|
||||
siteName: TITLE,
|
||||
images: [backdrop || poster['2x']],
|
||||
type: showType === 'movie' ? 'video.movie' : 'video.tv_show',
|
||||
url: `/${showType}/${id}`
|
||||
url: path
|
||||
}
|
||||
};
|
||||
} catch (_) {
|
||||
@@ -45,12 +50,18 @@ export const createShowPage = (showType: Show['type']) => {
|
||||
const ShowPage = async ({ params: { id } }: Props) => {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
const showId = Number(id);
|
||||
const showId = parseInt(id, 10);
|
||||
|
||||
if (!showId) return notFound();
|
||||
|
||||
const details = await queryClient.fetchQuery(detailsQuery({ showId, showType })).catch(() => null);
|
||||
|
||||
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',
|
||||
@@ -59,7 +70,7 @@ export const createShowPage = (showType: Show['type']) => {
|
||||
image: details.poster['2x'] || undefined,
|
||||
datePublished: details.release || undefined,
|
||||
genre: details.genres?.map(({ name }) => name),
|
||||
url: `${SITE_URL}/${showType}/${showId}`,
|
||||
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
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { createShowMetadata, createShowPage } from '../../showPage';
|
||||
|
||||
export const revalidate = 86400;
|
||||
|
||||
export const generateMetadata = createShowMetadata('tv');
|
||||
|
||||
const SeriesPage = createShowPage('tv');
|
||||
|
||||
@@ -13,6 +13,8 @@ interface Props {
|
||||
params: { id: string };
|
||||
}
|
||||
|
||||
export const revalidate = 86400;
|
||||
|
||||
export const generateMetadata = async ({ params: { id } }: Props): Promise<Metadata> => {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getServerSideSitemap, ISitemapField } from 'next-sitemap';
|
||||
|
||||
import { MOVIE_DB_API_URL, MOVIE_DB_TOKEN, SITE_URL } from '@/env';
|
||||
import { showPath } from '@/lib/utils';
|
||||
|
||||
export const revalidate = 86400;
|
||||
|
||||
@@ -15,9 +16,12 @@ const DISCOVER_FILTERS = {
|
||||
'vote_average.lte': '10'
|
||||
};
|
||||
|
||||
type TMDBListResponse = { results?: { id: number }[] };
|
||||
type TMDBListItem = { id: number; title?: string; name?: string };
|
||||
type TMDBListResponse = { results?: TMDBListItem[] };
|
||||
|
||||
const fetchIds = async (path: string, params: Record<string, string>): Promise<number[]> => {
|
||||
type Entry = { id: number; title: string };
|
||||
|
||||
const fetchEntries = async (path: string, params: Record<string, string>): Promise<Entry[]> => {
|
||||
const query = new URLSearchParams(params).toString();
|
||||
|
||||
const response = await fetch(`${MOVIE_DB_API_URL}/${path}?${query}`, {
|
||||
@@ -29,38 +33,42 @@ const fetchIds = async (path: string, params: Record<string, string>): Promise<n
|
||||
|
||||
const { results = [] }: TMDBListResponse = await response.json();
|
||||
|
||||
return results.map(({ id }) => id);
|
||||
return results.map(({ id, title, name }) => ({ id, title: title || name || '' }));
|
||||
};
|
||||
|
||||
// TMDB allows ~50 requests per second, so the task list is drained in small batches
|
||||
const collectIds = async (tasks: (() => Promise<number[]>)[]) => {
|
||||
const ids: number[] = [];
|
||||
const collectEntries = async (tasks: (() => Promise<Entry[]>)[]) => {
|
||||
const entries = new Map<number, string>();
|
||||
|
||||
for (let index = 0; index < tasks.length; index += BATCH_SIZE) {
|
||||
const batch = await Promise.all(tasks.slice(index, index + BATCH_SIZE).map((task) => task().catch(() => [])));
|
||||
|
||||
ids.push(...batch.flat());
|
||||
batch.flat().forEach(({ id, title }) => entries.has(id) || entries.set(id, title));
|
||||
}
|
||||
|
||||
return Array.from(new Set(ids));
|
||||
return Array.from(entries, ([id, title]) => ({ id, title }));
|
||||
};
|
||||
|
||||
const pages = (count: number) => Array.from({ length: count }, (_, index) => `${index + 1}`);
|
||||
|
||||
const discoverTasks = (type: Show['type']) =>
|
||||
['popularity.desc', 'vote_average.desc'].flatMap((sortBy) =>
|
||||
pages(LIST_PAGES).map((page) => () => fetchIds(`discover/${type}`, { ...DISCOVER_FILTERS, sort_by: sortBy, page }))
|
||||
pages(LIST_PAGES).map(
|
||||
(page) => () => fetchEntries(`discover/${type}`, { ...DISCOVER_FILTERS, sort_by: sortBy, page })
|
||||
)
|
||||
);
|
||||
|
||||
export async function GET() {
|
||||
const movieIds = await collectIds(discoverTasks('movie'));
|
||||
const seriesIds = await collectIds(discoverTasks('tv'));
|
||||
const personIds = await collectIds(pages(PERSON_PAGES).map((page) => () => fetchIds('person/popular', { page })));
|
||||
const movies = await collectEntries(discoverTasks('movie'));
|
||||
const series = await collectEntries(discoverTasks('tv'));
|
||||
const persons = await collectEntries(
|
||||
pages(PERSON_PAGES).map((page) => () => fetchEntries('person/popular', { page }))
|
||||
);
|
||||
|
||||
const fields: ISitemapField[] = [
|
||||
...movieIds.map((id) => `${SITE_URL}/movie/${id}`),
|
||||
...seriesIds.map((id) => `${SITE_URL}/tv/${id}`),
|
||||
...personIds.map((id) => `${SITE_URL}/person/${id}`)
|
||||
...movies.map(({ id, title }) => `${SITE_URL}${showPath({ type: 'movie', id, title })}`),
|
||||
...series.map(({ id, title }) => `${SITE_URL}${showPath({ type: 'tv', id, title })}`),
|
||||
...persons.map(({ id }) => `${SITE_URL}/person/${id}`)
|
||||
].map((loc) => ({ loc }));
|
||||
|
||||
return getServerSideSitemap(fields);
|
||||
|
||||
Reference in new issue
Block a user