-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from bitfreee/add-sitemap
Add sitemap.xml
- Loading branch information
Showing
5 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters