cg / hackerproducts
63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
src/app/sitemap.ts · 64 lines · 1.6 KB← back to folder
| 1 | import type { MetadataRoute } from "next"; |
| 2 | import { query, isDatabaseConfigured } from "@/db"; |
| 3 | import { getSiteUrl } from "@/lib/auth"; |
| 4 | |
| 5 | export const dynamic = "force-dynamic"; |
| 6 | |
| 7 | export default async function sitemap(): Promise<MetadataRoute.Sitemap> { |
| 8 | const siteUrl = getSiteUrl(); |
| 9 | const entries: MetadataRoute.Sitemap = [ |
| 10 | { |
| 11 | url: siteUrl, |
| 12 | lastModified: new Date(), |
| 13 | changeFrequency: "daily", |
| 14 | priority: 1, |
| 15 | }, |
| 16 | { |
| 17 | url: `${siteUrl}/makers`, |
| 18 | lastModified: new Date(), |
| 19 | changeFrequency: "daily", |
| 20 | priority: 0.7, |
| 21 | }, |
| 22 | ]; |
| 23 | |
| 24 | if (!isDatabaseConfigured()) { |
| 25 | return entries; |
| 26 | } |
| 27 | |
| 28 | try { |
| 29 | const products = await query<{ slug: string; updated_at: string }>( |
| 30 | `SELECT slug, updated_at FROM products WHERE status = 'published' ORDER BY published_at DESC LIMIT 5000` |
| 31 | ); |
| 32 | |
| 33 | for (const p of products.rows) { |
| 34 | entries.push({ |
| 35 | url: `${siteUrl}/${p.slug}`, |
| 36 | lastModified: new Date(p.updated_at), |
| 37 | changeFrequency: "weekly", |
| 38 | priority: 0.8, |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | const makers = await query<{ username: string; updated_at: string }>( |
| 43 | `SELECT DISTINCT u.username, u.updated_at |
| 44 | FROM users u |
| 45 | JOIN products p ON p.owner_id = u.id |
| 46 | WHERE p.status = 'published' |
| 47 | LIMIT 5000` |
| 48 | ); |
| 49 | |
| 50 | for (const m of makers.rows) { |
| 51 | entries.push({ |
| 52 | url: `${siteUrl}/${m.username}`, |
| 53 | lastModified: new Date(m.updated_at), |
| 54 | changeFrequency: "weekly", |
| 55 | priority: 0.6, |
| 56 | }); |
| 57 | } |
| 58 | } catch { |
| 59 | // Fallback to static entries if DB is temporarily unreachable |
| 60 | } |
| 61 | |
| 62 | return entries; |
| 63 | } |
| 64 |