This commit is contained in:
Matthias Hochmeister
2026-03-13 15:04:40 +01:00
parent 8d9388ca9a
commit 7833dca29c
3 changed files with 25 additions and 7 deletions

View File

@@ -86,8 +86,15 @@ function buildHeaders(): Record<string, string> {
* Fetches all BookStack books and returns a map of book_id → book_slug.
* The /api/pages list endpoint does not reliably include book_slug, so we
* look it up separately and use it when constructing page URLs.
* Cached for 5 minutes to avoid hammering the API on every dashboard load.
*/
let bookSlugMapCache: { map: Map<number, string>; expiresAt: number } | null = null;
const BOOK_SLUG_CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
async function getBookSlugMap(): Promise<Map<number, string>> {
if (bookSlugMapCache && Date.now() < bookSlugMapCache.expiresAt) {
return bookSlugMapCache.map;
}
const { bookstack } = environment;
try {
const response = await httpClient.get(
@@ -95,9 +102,11 @@ async function getBookSlugMap(): Promise<Map<number, string>> {
{ params: { count: 500 }, headers: buildHeaders() },
);
const books: Array<{ id: number; slug: string }> = response.data?.data ?? [];
return new Map(books.map((b) => [b.id, b.slug]));
const map = new Map(books.map((b) => [b.id, b.slug]));
bookSlugMapCache = { map, expiresAt: Date.now() + BOOK_SLUG_CACHE_TTL_MS };
return map;
} catch {
return new Map();
return bookSlugMapCache?.map ?? new Map();
}
}