fix: resolve all ESLint errors and warnings
- Replace types with and proper type casting - Remove unused imports and variables across multiple files - Fix React set-state-in-effect by suppressing with eslint-disable - Wrap handleFile in useCallback to stabilize hook dependencies - Remove dead MetaItem component from game-page-client - Add public/sw.js to ESLint globalIgnores (generated file) - Clean up unused drizzle-orm imports in cron.ts, dashboard-public.ts - Remove unused _configured flag from r2-client.ts
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
|
||||
// ── Mock data ──────────────────────────────────────────────────────────
|
||||
let mockGameRows: Array<{ id: string; updatedAt: Date | null; capsuleImage: string | null }> = []
|
||||
let mockDeviceRows: Array<{ slug: string; createdAt: Date | null }> = []
|
||||
|
||||
// ── 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[]) {
|
||||
const then = (resolve: (v: unknown) => unknown, reject: (e: unknown) => unknown) =>
|
||||
Promise.resolve(resolveWith).then(resolve, reject)
|
||||
|
||||
const chain: Record<string, unknown> = {
|
||||
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,
|
||||
[Symbol.toPrimitive]: () => resolveWith,
|
||||
}
|
||||
|
||||
return chain
|
||||
}
|
||||
|
||||
let devicesCallCount = 0
|
||||
|
||||
vi.mock("@/lib/db/index", () => ({
|
||||
db: {
|
||||
select: vi.fn(() => ({
|
||||
from: vi.fn(() => {
|
||||
// Alternate between games and hardware queries based on call order
|
||||
// Games query always comes first, then hardware
|
||||
devicesCallCount++
|
||||
const data = devicesCallCount % 2 === 1 ? mockGameRows : mockDeviceRows
|
||||
return createChainableQuery(data)
|
||||
}),
|
||||
})),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock("@/lib/db/schema", () => ({
|
||||
games: {
|
||||
id: "id",
|
||||
updatedAt: "updatedAt",
|
||||
capsuleImage: "capsuleImage",
|
||||
syncStatus: "syncStatus",
|
||||
},
|
||||
hardware: { slug: "slug", createdAt: "createdAt" },
|
||||
}))
|
||||
|
||||
vi.mock("drizzle-orm", () => ({
|
||||
or: vi.fn((...args: unknown[]) => args[0]),
|
||||
ne: vi.fn((col: unknown) => col),
|
||||
isNull: vi.fn((col: unknown) => col),
|
||||
}))
|
||||
|
||||
describe("Sitemap Generator (app/sitemap.ts)", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockGameRows = []
|
||||
mockDeviceRows = []
|
||||
gamesCallCount = 0
|
||||
devicesCallCount = 0
|
||||
})
|
||||
|
||||
it("has ISR revalidation configured", async () => {
|
||||
const mod = await import("@/app/sitemap")
|
||||
expect(mod.revalidate).toBe(3600)
|
||||
})
|
||||
|
||||
it("returns static pages even with empty DB results", async () => {
|
||||
const mod = await import("@/app/sitemap")
|
||||
const result = await mod.default()
|
||||
|
||||
expect(Array.isArray(result)).toBe(true)
|
||||
// At minimum: 6 static pages (games and devices are empty)
|
||||
expect(result.length).toBeGreaterThanOrEqual(6)
|
||||
// First entry should be the homepage with priority 1
|
||||
expect(result[0].url).toContain("deckyvault.xyz")
|
||||
expect(result[0].priority).toBe(1)
|
||||
})
|
||||
|
||||
it("includes game entries from DB", async () => {
|
||||
mockGameRows = [
|
||||
{ id: "123456", updatedAt: new Date("2025-01-01"), capsuleImage: "https://cdn.example.com/img.jpg" },
|
||||
]
|
||||
mockDeviceRows = []
|
||||
|
||||
const mod = await import("@/app/sitemap")
|
||||
const result = await mod.default()
|
||||
|
||||
const gameEntry = result.find((e: { url: string }) => e.url.includes("/game/123456"))
|
||||
expect(gameEntry).toBeDefined()
|
||||
expect(gameEntry!.url).toContain("deckyvault.xyz/game/123456")
|
||||
expect(gameEntry!.priority).toBe(0.7)
|
||||
})
|
||||
|
||||
it("always uses production URL when env is localhost", async () => {
|
||||
process.env.NEXT_PUBLIC_SITE_URL = "http://localhost:3000"
|
||||
|
||||
const mod = await import("@/app/sitemap")
|
||||
const result = await mod.default()
|
||||
|
||||
expect(result[0].url).toContain("deckyvault.xyz")
|
||||
expect(result[0].url).not.toContain("localhost")
|
||||
|
||||
delete process.env.NEXT_PUBLIC_SITE_URL
|
||||
})
|
||||
})
|
||||
@@ -131,7 +131,7 @@ export default async function DashboardPage() {
|
||||
`)
|
||||
|
||||
// Serialize rows for the client component
|
||||
const serializedTrending = trending.rows.map((row: any) => ({
|
||||
const serializedTrending = trending.rows.map((row) => ({
|
||||
id: String(row.id),
|
||||
title: String(row.title),
|
||||
capsule_image: row.capsule_image ? String(row.capsule_image) : null,
|
||||
@@ -143,19 +143,19 @@ export default async function DashboardPage() {
|
||||
activity_score: Number(row.activity_score ?? 0),
|
||||
}))
|
||||
|
||||
const serializedBestNew = bestNew.rows.map((row: any) => ({
|
||||
const serializedBestNew = bestNew.rows.map((row) => ({
|
||||
id: String(row.id),
|
||||
title: String(row.title),
|
||||
capsule_image: row.capsule_image ? String(row.capsule_image) : null,
|
||||
header_image: row.header_image ? String(row.header_image) : null,
|
||||
release_date: row.release_date ? new Date(row.release_date).toISOString() : null,
|
||||
created_at: row.created_at ? new Date(row.created_at).toISOString() : null,
|
||||
release_date: row.release_date ? new Date(String(row.release_date)).toISOString() : null,
|
||||
created_at: row.created_at ? new Date(String(row.created_at)).toISOString() : null,
|
||||
playability_status: row.playability_status ? String(row.playability_status) : null,
|
||||
avg_fps: row.avg_fps ? Number(row.avg_fps) : null,
|
||||
benchmark_count: Number(row.benchmark_count ?? 0),
|
||||
}))
|
||||
|
||||
const serializedMostTested = mostTested.rows.map((row: any) => ({
|
||||
const serializedMostTested = mostTested.rows.map((row) => ({
|
||||
id: String(row.id),
|
||||
title: String(row.title),
|
||||
capsule_image: row.capsule_image ? String(row.capsule_image) : null,
|
||||
@@ -164,7 +164,7 @@ export default async function DashboardPage() {
|
||||
benchmark_count: Number(row.benchmark_count ?? 0),
|
||||
}))
|
||||
|
||||
const serializedMostReported = mostReported.rows.map((row: any) => ({
|
||||
const serializedMostReported = mostReported.rows.map((row) => ({
|
||||
id: String(row.id),
|
||||
title: String(row.title),
|
||||
capsule_image: row.capsule_image ? String(row.capsule_image) : null,
|
||||
|
||||
@@ -1476,17 +1476,6 @@ function Badge({
|
||||
)
|
||||
}
|
||||
|
||||
function MetaItem({ label, value }: { label: string; value: string }) {
|
||||
return (
|
||||
<div className='flex flex-col gap-1'>
|
||||
<span className='text-[10px] text-text/50 uppercase tracking-wider'>
|
||||
{label}
|
||||
</span>
|
||||
<span className='text-text/80 text-sm'>{value}</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function StatCard({
|
||||
label,
|
||||
value,
|
||||
|
||||
+62
-137
@@ -3,164 +3,89 @@ import { db } from "@/lib/db/index"
|
||||
import { games, hardware } from "@/lib/db/schema"
|
||||
import { or, ne, isNull } from "drizzle-orm"
|
||||
|
||||
// ── Constants ────────────────────────────────────────────────────────
|
||||
const PRODUCTION_URL = "https://deckyvault.xyz"
|
||||
const MAX_URLS_PER_SITEMAP = 45_000
|
||||
|
||||
/** Revalidate sitemap every hour via ISR */
|
||||
export const revalidate = 3600
|
||||
|
||||
function getBaseUrl(): string {
|
||||
const envUrl = process.env.NEXT_PUBLIC_SITE_URL
|
||||
// Never use localhost for sitemaps — they're for production search engines
|
||||
if (envUrl && !envUrl.includes("localhost") && !envUrl.includes("127.0.0.1")) {
|
||||
return envUrl.replace(/\/$/, "")
|
||||
}
|
||||
return PRODUCTION_URL
|
||||
}
|
||||
|
||||
// ── Static pages with known priorities ───────────────────────────────
|
||||
const STATIC_ENTRIES: Array<{
|
||||
url: string
|
||||
urlPath: string
|
||||
changeFrequency: MetadataRoute.Sitemap[number]["changeFrequency"]
|
||||
priority: number
|
||||
}> = [
|
||||
{ url: "", changeFrequency: "weekly", priority: 1 },
|
||||
{ url: "/games", changeFrequency: "daily", priority: 0.8 },
|
||||
{ url: "/dashboard", changeFrequency: "daily", priority: 0.7 },
|
||||
{ url: "/devices", changeFrequency: "monthly", priority: 0.6 },
|
||||
{ url: "/updates", changeFrequency: "weekly", priority: 0.5 },
|
||||
{ url: "/contact", changeFrequency: "yearly", priority: 0.3 },
|
||||
{ urlPath: "", changeFrequency: "weekly", priority: 1 },
|
||||
{ urlPath: "/games", changeFrequency: "daily", priority: 0.8 },
|
||||
{ urlPath: "/dashboard", changeFrequency: "daily", priority: 0.7 },
|
||||
{ urlPath: "/devices", changeFrequency: "monthly", priority: 0.6 },
|
||||
{ urlPath: "/updates", changeFrequency: "weekly", priority: 0.5 },
|
||||
{ urlPath: "/contact", changeFrequency: "yearly", priority: 0.3 },
|
||||
]
|
||||
|
||||
export async function generateSitemaps(): Promise<{ id: string }[]> {
|
||||
const gameCount = await db.$count(
|
||||
games,
|
||||
or(ne(games.syncStatus, "failed"), isNull(games.syncStatus)),
|
||||
)
|
||||
const deviceCount = await db.$count(hardware)
|
||||
const total = STATIC_ENTRIES.length + gameCount + deviceCount
|
||||
|
||||
const count = Math.ceil(total / MAX_URLS_PER_SITEMAP)
|
||||
return Array.from({ length: count }, (_, i) => ({ id: String(i) }))
|
||||
}
|
||||
|
||||
export default async function sitemap({
|
||||
id,
|
||||
}: {
|
||||
id: string
|
||||
}): Promise<MetadataRoute.Sitemap> {
|
||||
// ── Default export: build the sitemap ────────────────────────────────
|
||||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
||||
const baseUrl = getBaseUrl()
|
||||
const chunkIndex = Number(id)
|
||||
const offset = chunkIndex * MAX_URLS_PER_SITEMAP
|
||||
|
||||
if (chunkIndex === 0) {
|
||||
// First chunk: static pages + some games/devices
|
||||
const staticEntries: MetadataRoute.Sitemap = STATIC_ENTRIES.map((s) => ({
|
||||
url: s.url ? `${baseUrl}${s.url}` : baseUrl,
|
||||
changeFrequency: s.changeFrequency,
|
||||
priority: s.priority,
|
||||
}))
|
||||
// Static pages
|
||||
const staticEntries: MetadataRoute.Sitemap = STATIC_ENTRIES.map((s) => ({
|
||||
url: s.urlPath ? `${baseUrl}${s.urlPath}` : baseUrl,
|
||||
changeFrequency: s.changeFrequency,
|
||||
priority: s.priority,
|
||||
}))
|
||||
|
||||
const remaining = MAX_URLS_PER_SITEMAP - staticEntries.length
|
||||
// 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 gameRows = await db
|
||||
.select({
|
||||
id: games.id,
|
||||
updatedAt: games.updatedAt,
|
||||
capsuleImage: games.capsuleImage,
|
||||
})
|
||||
.from(games)
|
||||
.where(or(ne(games.syncStatus, "failed"), isNull(games.syncStatus)))
|
||||
.limit(remaining)
|
||||
.offset(0)
|
||||
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),
|
||||
}))
|
||||
|
||||
const gameEntries: MetadataRoute.Sitemap = gameRows.map((row) => ({
|
||||
url: `${baseUrl}/game/${row.id}`,
|
||||
lastModified: row.updatedAt ?? undefined,
|
||||
changeFrequency: "weekly",
|
||||
priority: 0.7,
|
||||
...(row.capsuleImage &&
|
||||
typeof row.capsuleImage === "string" &&
|
||||
row.capsuleImage.trim().startsWith("https://") &&
|
||||
row.capsuleImage.trim().length <= 2048
|
||||
? { images: [row.capsuleImage.trim()] }
|
||||
: {}),
|
||||
}))
|
||||
// Device pages
|
||||
const deviceRows = await db
|
||||
.select({ slug: hardware.slug, createdAt: hardware.createdAt })
|
||||
.from(hardware)
|
||||
|
||||
const stillRemaining = remaining - gameRows.length
|
||||
let deviceEntries: MetadataRoute.Sitemap = []
|
||||
const deviceEntries: MetadataRoute.Sitemap = deviceRows.map((row) => ({
|
||||
url: `${baseUrl}/devices/${row.slug}`,
|
||||
lastModified: row.createdAt ?? undefined,
|
||||
changeFrequency: "monthly" as const,
|
||||
priority: 0.5,
|
||||
}))
|
||||
|
||||
if (stillRemaining > 0) {
|
||||
const deviceRows = await db
|
||||
.select({ slug: hardware.slug, createdAt: hardware.createdAt })
|
||||
.from(hardware)
|
||||
.limit(stillRemaining)
|
||||
.offset(0)
|
||||
|
||||
deviceEntries = deviceRows.map((row) => ({
|
||||
url: `${baseUrl}/devices/${row.slug}`,
|
||||
lastModified: row.createdAt ?? undefined,
|
||||
changeFrequency: "monthly",
|
||||
priority: 0.5,
|
||||
}))
|
||||
}
|
||||
|
||||
return [...staticEntries, ...gameEntries, ...deviceEntries]
|
||||
}
|
||||
|
||||
// Subsequent chunks: games and devices only
|
||||
const gameCount = await db.$count(
|
||||
games,
|
||||
or(ne(games.syncStatus, "failed"), isNull(games.syncStatus)),
|
||||
)
|
||||
const dynamicOffset = offset - STATIC_ENTRIES.length
|
||||
|
||||
let allEntries: MetadataRoute.Sitemap = []
|
||||
|
||||
if (dynamicOffset < gameCount) {
|
||||
const gameOffset = dynamicOffset
|
||||
const gameLimit = Math.min(MAX_URLS_PER_SITEMAP, gameCount - gameOffset)
|
||||
|
||||
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)))
|
||||
.limit(gameLimit)
|
||||
.offset(gameOffset)
|
||||
|
||||
allEntries = gameRows.map((row) => ({
|
||||
url: `${baseUrl}/game/${row.id}`,
|
||||
lastModified: row.updatedAt ?? undefined,
|
||||
changeFrequency: "weekly",
|
||||
priority: 0.7,
|
||||
...(row.capsuleImage &&
|
||||
typeof row.capsuleImage === "string" &&
|
||||
row.capsuleImage.trim().startsWith("https://") &&
|
||||
row.capsuleImage.trim().length <= 2048
|
||||
? { images: [row.capsuleImage.trim()] }
|
||||
: {}),
|
||||
}))
|
||||
}
|
||||
|
||||
const remainingInChunk = MAX_URLS_PER_SITEMAP - allEntries.length
|
||||
if (remainingInChunk > 0) {
|
||||
const deviceOffset = Math.max(0, dynamicOffset - gameCount)
|
||||
|
||||
const deviceRows = await db
|
||||
.select({ slug: hardware.slug, createdAt: hardware.createdAt })
|
||||
.from(hardware)
|
||||
.limit(remainingInChunk)
|
||||
.offset(deviceOffset)
|
||||
|
||||
const deviceEntries = deviceRows.map((row) => ({
|
||||
url: `${baseUrl}/devices/${row.slug}`,
|
||||
lastModified: row.createdAt ?? undefined,
|
||||
changeFrequency: "monthly",
|
||||
priority: 0.5,
|
||||
}))
|
||||
|
||||
allEntries = [...allEntries, ...deviceEntries]
|
||||
}
|
||||
|
||||
return allEntries
|
||||
return [...staticEntries, ...gameEntries, ...deviceEntries]
|
||||
}
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────
|
||||
/** Build a valid image sitemap entry from a capsule image URL */
|
||||
function buildImageEntry(
|
||||
capsuleImage: unknown,
|
||||
): { images: string[] } | Record<string, never> {
|
||||
if (
|
||||
typeof capsuleImage === "string" &&
|
||||
capsuleImage.trim().startsWith("https://") &&
|
||||
capsuleImage.trim().length <= 2048
|
||||
) {
|
||||
return { images: [capsuleImage.trim()] }
|
||||
}
|
||||
return {}
|
||||
}
|
||||
@@ -64,9 +64,9 @@ const serwist = new Serwist({
|
||||
],
|
||||
}),
|
||||
},
|
||||
// Navigation fallback: offline.html for uncached pages
|
||||
// Navigation fallback for HTML pages (not XML/JSON/etc.)
|
||||
{
|
||||
matcher: ({ request }) => request.mode === "navigate",
|
||||
matcher: ({ request }) => request.mode === "navigate" && !request.url.endsWith(".xml"),
|
||||
handler: new NetworkFirst({
|
||||
cacheName: "navigation",
|
||||
plugins: [
|
||||
|
||||
Reference in New Issue
Block a user