chore: merge dev -> prod - robust sitemap + error handling

This commit is contained in:
2026-05-23 15:37:30 +08:00
2 changed files with 237 additions and 78 deletions
+86 -16
View File
@@ -5,23 +5,12 @@ let mockGameRows: Array<{ id: string; updatedAt: Date | null; capsuleImage: stri
let mockDeviceRows: Array<{ slug: string; createdAt: Date | null }> = [] let mockDeviceRows: Array<{ slug: string; createdAt: Date | null }> = []
// ── Chainable query builder mock ────────────────────────────────────── // ── Chainable query builder mock ──────────────────────────────────────
// Drizzle ORM pattern: db.select().from(table).where(...).limit(...).offset(...)
// or without .where(): db.select().from(table)
// Both resolve as promises.
function createChainableQuery(resolveWith: unknown[]) { function createChainableQuery(resolveWith: unknown[]) {
const then = (resolve: (v: unknown) => unknown, reject: (e: unknown) => unknown) => const then = (resolve: (v: unknown) => unknown, reject: (e: unknown) => unknown) =>
Promise.resolve(resolveWith).then(resolve, reject) Promise.resolve(resolveWith).then(resolve, reject)
const chain: Record<string, unknown> = { const chain: Record<string, unknown> = {
where: vi.fn(() => ({ then, [Symbol.toPrimitive]: () => resolveWith })), where: vi.fn(() => ({ then, [Symbol.toPrimitive]: () => resolveWith })),
limit: vi.fn(() => ({ then, [Symbol.toPrimitive]: () => resolveWith })),
offset: vi.fn(() => ({
then,
where: chain.where,
limit: chain.limit,
[Symbol.toPrimitive]: () => resolveWith,
})),
then, then,
[Symbol.toPrimitive]: () => resolveWith, [Symbol.toPrimitive]: () => resolveWith,
} }
@@ -35,8 +24,6 @@ vi.mock("@/lib/db/index", () => ({
db: { db: {
select: vi.fn(() => ({ select: vi.fn(() => ({
from: vi.fn(() => { from: vi.fn(() => {
// Alternate between games and hardware queries based on call order
// Games query always comes first, then hardware
devicesCallCount++ devicesCallCount++
const data = devicesCallCount % 2 === 1 ? mockGameRows : mockDeviceRows const data = devicesCallCount % 2 === 1 ? mockGameRows : mockDeviceRows
return createChainableQuery(data) return createChainableQuery(data)
@@ -61,6 +48,11 @@ vi.mock("drizzle-orm", () => ({
isNull: vi.fn((col: unknown) => col), isNull: vi.fn((col: unknown) => col),
})) }))
// Mock the updates module (markdown-based changelogs)
vi.mock("@/lib/updates", () => ({
getAllUpdates: vi.fn(() => []),
}))
describe("Sitemap Generator (app/sitemap.ts)", () => { describe("Sitemap Generator (app/sitemap.ts)", () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks() vi.clearAllMocks()
@@ -79,13 +71,22 @@ describe("Sitemap Generator (app/sitemap.ts)", () => {
const result = await mod.default() const result = await mod.default()
expect(Array.isArray(result)).toBe(true) expect(Array.isArray(result)).toBe(true)
// At minimum: 5 static pages (games and devices are empty) // 7 static pages (now includes /compare and /search)
expect(result.length).toBeGreaterThanOrEqual(5) expect(result.length).toBeGreaterThanOrEqual(7)
// First entry should be the homepage with priority 1 // First entry should be the homepage with priority 1
expect(result[0].url).toContain("deckyvault.xyz") expect(result[0].url).toContain("deckyvault.xyz")
expect(result[0].priority).toBe(1) expect(result[0].priority).toBe(1)
}) })
it("includes compare and search in static pages", async () => {
const mod = await import("@/app/sitemap")
const result = await mod.default()
const urls = result.map((e: { url: string }) => e.url)
expect(urls).toContain("https://deckyvault.xyz/compare")
expect(urls).toContain("https://deckyvault.xyz/search")
})
it("includes game entries from DB", async () => { it("includes game entries from DB", async () => {
mockGameRows = [ mockGameRows = [
{ id: "123456", updatedAt: new Date("2025-01-01"), capsuleImage: "https://cdn.example.com/img.jpg" }, { id: "123456", updatedAt: new Date("2025-01-01"), capsuleImage: "https://cdn.example.com/img.jpg" },
@@ -98,7 +99,65 @@ describe("Sitemap Generator (app/sitemap.ts)", () => {
const gameEntry = result.find((e: { url: string }) => e.url.includes("/game/123456")) const gameEntry = result.find((e: { url: string }) => e.url.includes("/game/123456"))
expect(gameEntry).toBeDefined() expect(gameEntry).toBeDefined()
expect(gameEntry!.url).toContain("deckyvault.xyz/game/123456") expect(gameEntry!.url).toContain("deckyvault.xyz/game/123456")
expect(gameEntry!.priority).toBe(0.7) expect(gameEntry!.priority).toBe(0.8)
})
it("includes image entries for games with capsule images", async () => {
mockGameRows = [
{ id: "abc123", updatedAt: null, capsuleImage: "https://cdn.example.com/capsule.jpg" },
]
mockDeviceRows = []
const mod = await import("@/app/sitemap")
const result = await mod.default()
const gameEntry = result.find((e: { url: string }) => e.url.includes("/game/abc123"))
expect(gameEntry).toBeDefined()
expect(gameEntry!.images).toEqual(["https://cdn.example.com/capsule.jpg"])
})
it("handles game with null capsuleImage gracefully", async () => {
mockGameRows = [
{ id: "noimg", updatedAt: null, capsuleImage: null },
]
mockDeviceRows = []
const mod = await import("@/app/sitemap")
const result = await mod.default()
const gameEntry = result.find((e: { url: string }) => e.url.includes("/game/noimg"))
expect(gameEntry).toBeDefined()
expect(gameEntry!.images).toBeUndefined()
})
it("includes device entries from DB", async () => {
mockGameRows = []
mockDeviceRows = [
{ slug: "steam-deck-oled", createdAt: new Date("2025-03-01") },
]
const mod = await import("@/app/sitemap")
const result = await mod.default()
const deviceEntry = result.find((e: { url: string }) => e.url.includes("/devices/steam-deck-oled"))
expect(deviceEntry).toBeDefined()
expect(deviceEntry!.priority).toBe(0.6)
})
it("includes entries from all data sources combined", async () => {
mockGameRows = [
{ id: "game1", updatedAt: null, capsuleImage: null },
{ id: "game2", updatedAt: null, capsuleImage: null },
]
mockDeviceRows = [
{ slug: "device-1", createdAt: null },
]
const mod = await import("@/app/sitemap")
const result = await mod.default()
// 7 static + 2 games + 1 device = 10
expect(result.length).toBeGreaterThanOrEqual(10)
}) })
it("always uses production URL when env is localhost", async () => { it("always uses production URL when env is localhost", async () => {
@@ -112,4 +171,15 @@ describe("Sitemap Generator (app/sitemap.ts)", () => {
delete process.env.NEXT_PUBLIC_SITE_URL delete process.env.NEXT_PUBLIC_SITE_URL
}) })
it("uses custom NEXT_PUBLIC_SITE_URL when set to non-localhost", async () => {
process.env.NEXT_PUBLIC_SITE_URL = "https://staging.deckyvault.xyz"
const mod = await import("@/app/sitemap")
const result = await mod.default()
expect(result[0].url).toContain("staging.deckyvault.xyz")
delete process.env.NEXT_PUBLIC_SITE_URL
})
}) })
+149 -60
View File
@@ -2,13 +2,20 @@ import type { MetadataRoute } from "next"
import { db } from "@/lib/db/index" import { db } from "@/lib/db/index"
import { games, hardware } from "@/lib/db/schema" import { games, hardware } from "@/lib/db/schema"
import { or, ne, isNull } from "drizzle-orm" import { or, ne, isNull } from "drizzle-orm"
import { getAllUpdates } from "@/lib/updates"
// ─── Configuration ─────────────────────────────────────────────────────
// ── Constants ────────────────────────────────────────────────────────
const PRODUCTION_URL = "https://deckyvault.xyz" const PRODUCTION_URL = "https://deckyvault.xyz"
/** Revalidate sitemap every hour via ISR */ /** Revalidate sitemap every hour via ISR */
export const revalidate = 3600 export const revalidate = 3600
/** Maximum entries per sitemap (Google's limit is 50k; we stay well under) */
const MAX_ENTRIES = 45_000
// ─── Helpers ────────────────────────────────────────────────────────────
function getBaseUrl(): string { function getBaseUrl(): string {
const envUrl = process.env.NEXT_PUBLIC_SITE_URL const envUrl = process.env.NEXT_PUBLIC_SITE_URL
if (envUrl && !envUrl.includes("localhost") && !envUrl.includes("127.0.0.1")) { if (envUrl && !envUrl.includes("localhost") && !envUrl.includes("127.0.0.1")) {
@@ -17,66 +24,8 @@ function getBaseUrl(): string {
return PRODUCTION_URL return PRODUCTION_URL
} }
// ── Static pages with known priorities ───────────────────────────────
const STATIC_ENTRIES: Array<{
urlPath: string
changeFrequency: MetadataRoute.Sitemap[number]["changeFrequency"]
priority: number
}> = [
{ urlPath: "", changeFrequency: "weekly", priority: 1 },
{ urlPath: "/games", changeFrequency: "daily", priority: 0.8 },
{ urlPath: "/devices", changeFrequency: "monthly", priority: 0.6 },
{ urlPath: "/updates", changeFrequency: "weekly", priority: 0.5 },
{ urlPath: "/contact", changeFrequency: "yearly", priority: 0.3 },
]
// ── Default export: build the sitemap ────────────────────────────────
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const baseUrl = getBaseUrl()
// Static pages
const staticEntries: MetadataRoute.Sitemap = STATIC_ENTRIES.map((s) => ({
url: s.urlPath ? `${baseUrl}${s.urlPath}` : baseUrl,
changeFrequency: s.changeFrequency,
priority: s.priority,
}))
// Game pages
const gameRows = await db
.select({
id: games.id,
updatedAt: games.updatedAt,
capsuleImage: games.capsuleImage,
})
.from(games)
.where(or(ne(games.syncStatus, "failed"), isNull(games.syncStatus)))
const gameEntries: MetadataRoute.Sitemap = gameRows.map((row) => ({
url: `${baseUrl}/game/${row.id}`,
lastModified: row.updatedAt ?? undefined,
changeFrequency: "weekly" as const,
priority: 0.7,
...buildImageEntry(row.capsuleImage),
}))
// Device pages
const deviceRows = await db
.select({ slug: hardware.slug, createdAt: hardware.createdAt })
.from(hardware)
const deviceEntries: MetadataRoute.Sitemap = deviceRows.map((row) => ({
url: `${baseUrl}/devices/${row.slug}`,
lastModified: row.createdAt ?? undefined,
changeFrequency: "monthly" as const,
priority: 0.5,
}))
return [...staticEntries, ...gameEntries, ...deviceEntries]
}
// ── Helpers ──────────────────────────────────────────────────────────
/** Build a valid image sitemap entry from a capsule image URL */ /** Build a valid image sitemap entry from a capsule image URL */
function buildImageEntry( function imageEntry(
capsuleImage: unknown, capsuleImage: unknown,
): { images: string[] } | Record<string, never> { ): { images: string[] } | Record<string, never> {
if ( if (
@@ -88,3 +37,143 @@ function buildImageEntry(
} }
return {} return {}
} }
/** Safely extract a Date from a value that could be Date, string, or nullish */
function toDate(value: unknown): Date | undefined {
if (value instanceof Date && !Number.isNaN(value.getTime())) return value
if (typeof value === "string" || typeof value === "number") {
const d = new Date(value)
if (!Number.isNaN(d.getTime())) return d
}
return undefined
}
/**
* Run a DB query with a safety net.
* Returns rows on success, undefined on failure — the sitemap still
* renders with whatever data is available.
*/
async function querySafe<T>(
label: string,
query: () => Promise<T>,
timeoutMs = 15_000,
): Promise<T | undefined> {
try {
const result = await Promise.race([
query(),
new Promise<never>((_, reject) =>
setTimeout(
() => reject(new Error(`[Sitemap] ${label} query timed out after ${timeoutMs}ms`)),
timeoutMs,
),
),
])
return result
} catch (err) {
console.error(`[Sitemap] ${label} query failed:`, err)
return undefined
}
}
// ─── Static page definitions ───────────────────────────────────────────
interface StaticPageDef {
urlPath: string
changeFrequency: MetadataRoute.Sitemap[number]["changeFrequency"]
priority: number
}
const STATIC_PAGES: StaticPageDef[] = [
// Homepage
{ urlPath: "", changeFrequency: "weekly", priority: 1.0 },
// Core browse pages
{ urlPath: "/games", changeFrequency: "daily", priority: 0.9 },
{ urlPath: "/devices", changeFrequency: "weekly", priority: 0.7 },
{ urlPath: "/updates", changeFrequency: "weekly", priority: 0.6 },
// Utility pages
{ urlPath: "/compare", changeFrequency: "weekly", priority: 0.5 },
{ urlPath: "/search", changeFrequency: "monthly", priority: 0.3 },
// Static content
{ urlPath: "/contact", changeFrequency: "yearly", priority: 0.3 },
]
// ─── Sitemap builder (called by Next.js on every request + ISR) ────────
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const baseUrl = getBaseUrl()
const entries: MetadataRoute.Sitemap = []
// ── 1. Static pages ────────────────────────────────────────────────
for (const page of STATIC_PAGES) {
entries.push({
url: page.urlPath ? `${baseUrl}${page.urlPath}` : baseUrl,
changeFrequency: page.changeFrequency,
priority: page.priority,
})
}
// ── 2. Game detail pages (from database) ───────────────────────────
const gameRows = await querySafe("games", () =>
db
.select({
id: games.id,
updatedAt: games.updatedAt,
capsuleImage: games.capsuleImage,
})
.from(games)
.where(or(ne(games.syncStatus, "failed"), isNull(games.syncStatus))),
)
if (gameRows) {
for (const row of gameRows) {
if (entries.length >= MAX_ENTRIES) break
entries.push({
url: `${baseUrl}/game/${row.id}`,
lastModified: toDate(row.updatedAt),
changeFrequency: "weekly",
priority: 0.8,
...imageEntry(row.capsuleImage),
})
}
}
// ── 3. Device detail pages (from database) ─────────────────────────
const deviceRows = await querySafe("hardware", () =>
db
.select({
slug: hardware.slug,
createdAt: hardware.createdAt,
})
.from(hardware),
)
if (deviceRows) {
for (const row of deviceRows) {
if (entries.length >= MAX_ENTRIES) break
entries.push({
url: `${baseUrl}/devices/${row.slug}`,
lastModified: toDate(row.createdAt),
changeFrequency: "monthly",
priority: 0.6,
})
}
}
// ── 4. Update / changelog detail pages (from markdown files) ───────
try {
const updates = getAllUpdates()
for (const update of updates) {
if (entries.length >= MAX_ENTRIES) break
entries.push({
url: `${baseUrl}/updates/${update.slug}`,
lastModified: toDate(update.date),
changeFrequency: "monthly",
priority: 0.5,
})
}
} catch (err) {
console.error("[Sitemap] Failed to load updates:", err)
}
return entries
}