|
| 1 | +import type { RequestHandler } from '@sveltejs/kit'; |
| 2 | + |
| 3 | +const ORIGIN = 'https://app.trakt.tv'; |
| 4 | + |
| 5 | +type SitemapEntry = { |
| 6 | + path: string; |
| 7 | + priority: string; |
| 8 | + changefreq: |
| 9 | + | 'always' |
| 10 | + | 'hourly' |
| 11 | + | 'daily' |
| 12 | + | 'weekly' |
| 13 | + | 'monthly' |
| 14 | + | 'yearly' |
| 15 | + | 'never'; |
| 16 | +}; |
| 17 | + |
| 18 | +const STATIC_ROUTES: ReadonlyArray<SitemapEntry> = [ |
| 19 | + { path: '/', priority: '1.0', changefreq: 'daily' }, |
| 20 | + { path: '/movies', priority: '0.9', changefreq: 'daily' }, |
| 21 | + { path: '/movies/popular', priority: '0.8', changefreq: 'daily' }, |
| 22 | + { path: '/movies/trending', priority: '0.8', changefreq: 'hourly' }, |
| 23 | + { path: '/movies/anticipated', priority: '0.7', changefreq: 'weekly' }, |
| 24 | + { path: '/movies/recommended', priority: '0.7', changefreq: 'daily' }, |
| 25 | + { path: '/shows', priority: '0.9', changefreq: 'daily' }, |
| 26 | + { path: '/shows/popular', priority: '0.8', changefreq: 'daily' }, |
| 27 | + { path: '/shows/trending', priority: '0.8', changefreq: 'hourly' }, |
| 28 | + { path: '/shows/anticipated', priority: '0.7', changefreq: 'weekly' }, |
| 29 | + { path: '/shows/recommended', priority: '0.7', changefreq: 'daily' }, |
| 30 | + { path: '/media/popular', priority: '0.7', changefreq: 'daily' }, |
| 31 | + { path: '/media/trending', priority: '0.7', changefreq: 'hourly' }, |
| 32 | + { path: '/media/anticipated', priority: '0.7', changefreq: 'weekly' }, |
| 33 | + { path: '/media/recommended', priority: '0.7', changefreq: 'daily' }, |
| 34 | + { path: '/discover', priority: '0.8', changefreq: 'daily' }, |
| 35 | + { path: '/search', priority: '0.6', changefreq: 'monthly' }, |
| 36 | + { path: '/vip', priority: '0.5', changefreq: 'monthly' }, |
| 37 | + { path: '/about', priority: '0.4', changefreq: 'monthly' }, |
| 38 | + { path: '/privacy', priority: '0.3', changefreq: 'monthly' }, |
| 39 | + { path: '/terms', priority: '0.3', changefreq: 'monthly' }, |
| 40 | +]; |
| 41 | + |
| 42 | +const buildUrl = ( |
| 43 | + { path, priority, changefreq }: SitemapEntry, |
| 44 | + lastmod: string, |
| 45 | +) => |
| 46 | + ` <url> |
| 47 | + <loc>${ORIGIN}${path}</loc> |
| 48 | + <lastmod>${lastmod}</lastmod> |
| 49 | + <changefreq>${changefreq}</changefreq> |
| 50 | + <priority>${priority}</priority> |
| 51 | + </url>`; |
| 52 | + |
| 53 | +const buildSitemap = (lastmod: string) => { |
| 54 | + const urls = STATIC_ROUTES.map((entry) => buildUrl(entry, lastmod)).join( |
| 55 | + '\n', |
| 56 | + ); |
| 57 | + |
| 58 | + return `<?xml version="1.0" encoding="UTF-8"?> |
| 59 | +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 60 | +${urls} |
| 61 | +</urlset>`; |
| 62 | +}; |
| 63 | + |
| 64 | +export const GET: RequestHandler = () => { |
| 65 | + const lastmod = new Date().toISOString().split('T')[0]!; |
| 66 | + |
| 67 | + return new Response(buildSitemap(lastmod), { |
| 68 | + headers: { |
| 69 | + 'Content-Type': 'application/xml; charset=utf-8', |
| 70 | + 'Cache-Control': 'public, max-age=3600, s-maxage=3600', |
| 71 | + }, |
| 72 | + }); |
| 73 | +}; |
0 commit comments