|
| 1 | +import type { Metadata } from 'next' |
| 2 | +import process from 'node:process' |
| 3 | +import AnimeList from '@/components/anime/AnimeList' |
| 4 | +import { AnimeResponseSchema } from '@/schemas/anime' |
| 5 | + |
| 6 | +import { getConfig } from '@/services/config' |
| 7 | +import Head from 'next/head' |
| 8 | +import { notFound } from 'next/navigation' |
| 9 | + |
| 10 | +export async function generateMetadata(): Promise<Metadata> { |
| 11 | + const config = getConfig() |
| 12 | + const animeTranslation = config.translation.anime |
| 13 | + |
| 14 | + return { |
| 15 | + title: `${animeTranslation.title} - ${config.title}`, |
| 16 | + description: `${config.title}${animeTranslation.description} - ${config.description}`, |
| 17 | + alternates: { canonical: `${config.siteUrl}/about/anime` }, |
| 18 | + openGraph: { |
| 19 | + siteName: config.title, |
| 20 | + title: `${animeTranslation.title} - ${config.title}`, |
| 21 | + description: `${config.title}${animeTranslation.description} - ${config.description}`, |
| 22 | + url: '/about/anime', |
| 23 | + images: config.avatar, |
| 24 | + type: 'website', |
| 25 | + locale: config.lang, |
| 26 | + }, |
| 27 | + twitter: { |
| 28 | + card: 'summary', |
| 29 | + title: `${animeTranslation.title} - ${config.title}`, |
| 30 | + description: `${config.title}${animeTranslation.description} - ${config.description}`, |
| 31 | + images: config.avatar, |
| 32 | + }, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export default async function AnimePage() { |
| 37 | + const config = getConfig() |
| 38 | + const anilist_username = config.anilist_username |
| 39 | + |
| 40 | + if (anilist_username === undefined || anilist_username === null) { |
| 41 | + return notFound() |
| 42 | + } |
| 43 | + |
| 44 | + const API_BASE_URL = process.env.NODE_ENV === 'production' |
| 45 | + ? config.siteUrl |
| 46 | + : 'http://localhost:3000' |
| 47 | + |
| 48 | + const response = await fetch(`${API_BASE_URL}/api/anime?userName=${anilist_username}`, { |
| 49 | + cache: 'force-cache', |
| 50 | + next: { tags: ['anime'], revalidate: 120 }, // Cache for 2 minutes |
| 51 | + }) |
| 52 | + |
| 53 | + if (!response.ok) { |
| 54 | + console.error(`Failed to fetch anime data: ${response.statusText}`) |
| 55 | + return notFound() |
| 56 | + } |
| 57 | + |
| 58 | + const data = await response.json() as unknown |
| 59 | + |
| 60 | + const parsedAnimeData = AnimeResponseSchema.safeParse(data) |
| 61 | + |
| 62 | + if (parsedAnimeData.success === false) { |
| 63 | + console.error(`Zod validation failed: ${JSON.stringify(parsedAnimeData.error.format())}`) |
| 64 | + return notFound() |
| 65 | + } |
| 66 | + |
| 67 | + const animeData = parsedAnimeData.data |
| 68 | + |
| 69 | + const animeTranslation = config.translation.anime |
| 70 | + const jsonLd = { |
| 71 | + '@context': 'https://schema.org', |
| 72 | + '@type': 'WebSite', |
| 73 | + 'name': config.author.name, |
| 74 | + 'description': `${config.title}${animeTranslation.description} - ${config.description}`, |
| 75 | + 'url': `${config.siteUrl}/about/anime`, |
| 76 | + 'image': config.avatar, |
| 77 | + 'sameAs': config.author.link, |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <Head> |
| 83 | + <script |
| 84 | + type="application/ld+json" |
| 85 | + dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }} |
| 86 | + /> |
| 87 | + </Head> |
| 88 | + <AnimeList |
| 89 | + animeData={animeData} |
| 90 | + userName={anilist_username} |
| 91 | + author={config.author.name} |
| 92 | + lang={config.lang} |
| 93 | + translation={config.translation} |
| 94 | + /> |
| 95 | + </> |
| 96 | + ) |
| 97 | +} |
0 commit comments