Migrate sitemap to Next.js native app/sitemap.ts (#258)
* sitemap migration to next * robots.txt migration * rss refactoring * claude upd * add test
This commit is contained in:
1 parent
0fd75a3b9a
commit
05f7d4d13d
17 files changed
+214
-161
No files matched your search
@@ -1 +1,2 @@
|
||||
export const yearRange = [2012, 2026].join("—")
|
||||
export const siteUrl = "https://art.shlisselburg.org"
|
||||
@@ -0,0 +1,67 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import { apiGet, getRecentPages } from "./api"
|
||||
|
||||
function mockFetchJson(data: unknown, ok = true) {
|
||||
return vi.spyOn(globalThis, "fetch").mockResolvedValue({
|
||||
ok,
|
||||
json: async () => data,
|
||||
} as Response)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe("apiGet", () => {
|
||||
it("should call factory with fetched data on success", async () => {
|
||||
const data = { value: 1 }
|
||||
mockFetchJson(data)
|
||||
const factory = vi.fn((d: typeof data) => d.value)
|
||||
const result = await apiGet(factory)("https://example.com/api", () => -1)
|
||||
expect(factory).toHaveBeenCalledWith(data)
|
||||
expect(result).toBe(1)
|
||||
})
|
||||
|
||||
it("should return default on non-ok response", async () => {
|
||||
mockFetchJson(null, false)
|
||||
const factory = vi.fn()
|
||||
const result = await apiGet(factory)("https://example.com/api", () => "default")
|
||||
expect(factory).not.toHaveBeenCalled()
|
||||
expect(result).toBe("default")
|
||||
})
|
||||
|
||||
it("should return default and log error on network failure", async () => {
|
||||
vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("network"))
|
||||
const spy = vi.spyOn(console, "error").mockImplementation(() => {})
|
||||
const factory = vi.fn()
|
||||
const result = await apiGet(factory)("https://example.com/api", () => "fallback")
|
||||
expect(factory).not.toHaveBeenCalled()
|
||||
expect(result).toBe("fallback")
|
||||
expect(spy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("getRecentPages", () => {
|
||||
it("should fetch with default limit of 30", async () => {
|
||||
const spy = mockFetchJson([])
|
||||
await getRecentPages()
|
||||
expect(spy).toHaveBeenCalledWith(
|
||||
"https://hudozka.tmshv.com/pages?_sort=date:DESC&_limit=30",
|
||||
)
|
||||
})
|
||||
|
||||
it("should fetch with custom limit", async () => {
|
||||
const spy = mockFetchJson([])
|
||||
await getRecentPages(10)
|
||||
expect(spy).toHaveBeenCalledWith(
|
||||
"https://hudozka.tmshv.com/pages?_sort=date:DESC&_limit=10",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return empty array on failure", async () => {
|
||||
vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("fail"))
|
||||
vi.spyOn(console, "error").mockImplementation(() => {})
|
||||
const result = await getRecentPages()
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
})
|
||||
+8
-2
@@ -1,5 +1,5 @@
|
||||
import { createHomeCards, createMenu, createPage, createPageUrls } from "@/remote/factory"
|
||||
import type { MenuItem, Page, PageCardDto } from "@/types"
|
||||
import { createHomeCards, createMenu, createPage, createPageUrls, createFeedPages } from "@/remote/factory"
|
||||
import type { MenuItem, Page, PageCardDto, FeedPage } from "@/types"
|
||||
|
||||
const backendUrl = "https://hudozka.tmshv.com"
|
||||
|
||||
@@ -62,3 +62,9 @@ export async function getMenu(): Promise<MenuItem[]> {
|
||||
return menu
|
||||
}
|
||||
|
||||
export async function getRecentPages(limit: number = 30): Promise<FeedPage[]> {
|
||||
const url = `${backendUrl}/pages?_sort=date:DESC&_limit=${limit}`
|
||||
const pages = await apiGet(createFeedPages)(url, () => [])
|
||||
return pages
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { createFeedPages } from "./factory"
|
||||
import type { StrapiPage, StrapiTag } from "./types"
|
||||
|
||||
function stubPage(overrides: Partial<StrapiPage> = {}): StrapiPage {
|
||||
return {
|
||||
id: 1,
|
||||
title: "Test",
|
||||
excerpt: "Excerpt",
|
||||
slug: "/test",
|
||||
date: "2024-01-15",
|
||||
published_at: "",
|
||||
created_at: "",
|
||||
updated_at: "",
|
||||
content: [],
|
||||
tags: [] as StrapiTag[],
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe("createFeedPages", () => {
|
||||
it("should map strapi pages to feed pages", () => {
|
||||
const pages = [stubPage({ id: 42, title: "Hello", slug: "/hello", date: "2024-03-01", excerpt: "Summary" })]
|
||||
const result = createFeedPages(pages)
|
||||
expect(result).toEqual([{
|
||||
id: "42",
|
||||
title: "Hello",
|
||||
url: "/hello",
|
||||
date: "2024-03-01",
|
||||
excerpt: "Summary",
|
||||
}])
|
||||
})
|
||||
|
||||
it("should filter out pages without date", () => {
|
||||
const pages = [
|
||||
stubPage({ id: 1, date: "2024-01-01" }),
|
||||
stubPage({ id: 2, date: null }),
|
||||
]
|
||||
const result = createFeedPages(pages)
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].id).toBe("1")
|
||||
})
|
||||
|
||||
it("should return empty array for empty input", () => {
|
||||
expect(createFeedPages([])).toEqual([])
|
||||
})
|
||||
|
||||
it("should convert numeric id to string", () => {
|
||||
const result = createFeedPages([stubPage({ id: 99 })])
|
||||
expect(result[0].id).toBe("99")
|
||||
})
|
||||
})
|
||||
+13
-1
@@ -1,4 +1,4 @@
|
||||
import type { MenuItem, Page, Tag, PageCardDto, Pic, Token } from "@/types"
|
||||
import type { MenuItem, Page, Tag, PageCardDto, Pic, Token, FeedPage } from "@/types"
|
||||
import { asItem } from "./lib"
|
||||
import type { StrapiComponentEmbed, StrapiComponent, StrapiHome, StrapiPage, StrapiPageCard, StrapiTag, StrapiMedia, StrapiMenu } from "./types"
|
||||
import { typograf, markdownToHtml } from "@/lib/text"
|
||||
@@ -213,3 +213,15 @@ export async function createHomeCards(data: StrapiHome): Promise<PageCardDto[]>
|
||||
return items
|
||||
.filter(x => !!x.url)
|
||||
}
|
||||
|
||||
export function createFeedPages(pages: StrapiPage[]): FeedPage[] {
|
||||
return pages
|
||||
.filter(page => page.date)
|
||||
.map(page => ({
|
||||
id: `${page.id}`,
|
||||
title: page.title,
|
||||
url: page.slug,
|
||||
date: page.date!,
|
||||
excerpt: page.excerpt,
|
||||
}))
|
||||
}
|
||||
@@ -156,3 +156,11 @@ export type Token =
|
||||
| YoutubeToken
|
||||
| InstagramToken
|
||||
| GridToken
|
||||
|
||||
export type FeedPage = {
|
||||
id: string
|
||||
title: string
|
||||
url: string
|
||||
date: string
|
||||
excerpt: string
|
||||
}
|
||||
Reference in new issue
Block a user