Skip to content

Commit

Permalink
Merge pull request #21 from bitfreee/add-sitemap
Browse files Browse the repository at this point in the history
Add sitemap.xml
  • Loading branch information
bitfreee authored Aug 23, 2024
2 parents 6b599e9 + f7dbc5c commit d000751
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/app/sitemap.xml/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { env } from '@/env.mjs';
export function GET() {
const sitemapIndices: string[] = [];
// tmdb has a limit of 500 pages
for (let i = 0; i < 501; i++) {
sitemapIndices.push(
`
<sitemap>
<loc>${env.NEXT_PUBLIC_APP_URL}/sitemap/${i}.xml</loc>
</sitemap>
`,
);
}
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${sitemapIndices.join('')}
</sitemapindex>
`;

return new Response(sitemap, {
status: 200,
headers: { 'Content-Type': 'text/xml' },
});
}
38 changes: 38 additions & 0 deletions src/app/sitemap/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { env } from '@/env.mjs';
import { buildMovieUrl } from '@/lib/utils';
import { getTrendingAll } from '@/services/MovieService/tmdbService';

export async function GET(request: Request, ctx: { params: { id: string } }) {
if (!ctx.params.id) return new Response('Not found', { status: 404 });
const id = parseInt(ctx.params.id.replace('.xml', ''));
let urls: string[] = [];
if (id === 0) {
urls = [
`${env.NEXT_PUBLIC_APP_URL}`,
`${env.NEXT_PUBLIC_APP_URL}/home`,
`${env.NEXT_PUBLIC_APP_URL}/tv-shows`,
`${env.NEXT_PUBLIC_APP_URL}/movies`,
`${env.NEXT_PUBLIC_APP_URL}/new-and-popular`,
];
} else {
const data = await getTrendingAll(id);
data.results.forEach((show) => urls.push(buildMovieUrl(show)));
}
const sitemap = `<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
${urls
.map((url) => {
return `
<url>
<loc>${url}</loc>
</url>
`;
})
.join('')}
</urlset>
`;

return new Response(sitemap, {
status: 200,
headers: { 'Content-Type': 'text/xml' },
});
}
16 changes: 15 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { siteConfig } from '@/configs/site';
import { env } from '@/env.mjs';
import MovieService from '@/services/MovieService';
import type { CategorizedShows, KeyWord, KeyWordResponse, Show } from '@/types';
import {
MediaType,
type CategorizedShows,
type KeyWord,
type KeyWordResponse,
type Show,
} from '@/types';
import { type AxiosResponse } from 'axios';
import { clsx, type ClassValue } from 'clsx';
import { cache } from 'react';
Expand Down Expand Up @@ -50,6 +56,14 @@ export function getSlug(id: number, name: string): string {
return `${name.toLowerCase().replace(regex, '-')}-${id}`;
}

export function buildMovieUrl(show: Show): string {
const name = getNameFromShow(show);
const id = show.id;
return `${env.NEXT_PUBLIC_APP_URL}/${
show.media_type === MediaType.MOVIE ? 'movies' : 'tv-shows'
}/${getSlug(id, name)}`;
}

export function getIdFromSlug(slug: string): number {
// get id from slug
const id: string | undefined = slug.split('-').pop();
Expand Down
7 changes: 7 additions & 0 deletions src/services/MovieService/tmdbService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ export async function getMoviesByGenre(
data.results.forEach((movie) => (movie.media_type = mediaType));
return data;
}

export async function getTrendingAll(page: number) {
const { data } = await tmdbClient.get<TmdbPagingResponse>(
`/trending/all/week?language=en-US&page=${page}`,
);
return data;
}
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"**/*.tsx",
"**/*.cjs",
"**/*.mjs",
".next/types/**/*.ts"
".next/types/**/*.ts",
"src/app/sitemap.xml",
"src/app/sitemap/[id]"
],
"exclude": ["node_modules"]
}

0 comments on commit d000751

Please sign in to comment.