From ecce0a97387ae9f01e4e3425d8ce711fac0556b2 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Sat, 28 Feb 2026 10:16:12 +0100 Subject: [PATCH] fix: invalidate home cache when updating read progress - Add cache tags support to BaseApiService - Tag home data with 'home-data' tag in HomeService - Use revalidateTag('home-data', 'max') after read progress updates - With 'max' profile: serve stale while fetching fresh in background --- .../books/[bookId]/read-progress/route.ts | 9 ++++++ src/lib/services/base-api.service.ts | 32 ++++++++++++------- src/lib/services/home.service.ts | 16 ++++++---- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/app/api/komga/books/[bookId]/read-progress/route.ts b/src/app/api/komga/books/[bookId]/read-progress/route.ts index ae5875b..ef16a8d 100644 --- a/src/app/api/komga/books/[bookId]/read-progress/route.ts +++ b/src/app/api/komga/books/[bookId]/read-progress/route.ts @@ -1,11 +1,14 @@ import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; +import { revalidateTag } from "next/cache"; import { BookService } from "@/lib/services/book.service"; import { ERROR_CODES } from "@/constants/errorCodes"; import { getErrorMessage } from "@/utils/errors"; import { AppError } from "@/utils/errors"; import logger from "@/lib/logger"; +const HOME_CACHE_TAG = "home-data"; + export async function PATCH( request: NextRequest, { params }: { params: Promise<{ bookId: string }> } @@ -60,6 +63,9 @@ export async function PATCH( await BookService.updateReadProgress(bookId, page, completed); + // Invalider le cache de la home via le tag + revalidateTag(HOME_CACHE_TAG, "max"); + return NextResponse.json({ message: "📖 Progression mise à jour avec succès" }); } catch (error) { logger.error({ err: error }, "Erreur lors de la mise à jour de la progression:"); @@ -97,6 +103,9 @@ export async function DELETE( await BookService.deleteReadProgress(bookId); + // Invalider le cache de la home via le tag + revalidateTag(HOME_CACHE_TAG, "max"); + return NextResponse.json({ message: "🗑️ Progression supprimée avec succès" }); } catch (error) { logger.error({ err: error }, "Erreur lors de la suppression de la progression:"); diff --git a/src/lib/services/base-api.service.ts b/src/lib/services/base-api.service.ts index 199efcc..321a20b 100644 --- a/src/lib/services/base-api.service.ts +++ b/src/lib/services/base-api.service.ts @@ -10,6 +10,8 @@ interface KomgaRequestInit extends RequestInit { noJson?: boolean; /** Next.js cache duration in seconds. Use false to disable cache, number for TTL */ revalidate?: number | false; + /** Cache tags for targeted invalidation */ + tags?: string[]; } interface KomgaUrlBuilder { @@ -137,10 +139,12 @@ export abstract class BaseApiService { connectTimeout: timeoutMs, bodyTimeout: timeoutMs, headersTimeout: timeoutMs, - // Next.js cache - next: options.revalidate !== undefined - ? { revalidate: options.revalidate } - : undefined, + // Next.js cache with tags support + next: options.tags + ? { tags: options.tags } + : options.revalidate !== undefined + ? { revalidate: options.revalidate } + : undefined, }); } catch (fetchError: any) { // Gestion spécifique des erreurs DNS @@ -158,10 +162,12 @@ export abstract class BaseApiService { // Force IPv4 si IPv6 pose problème // @ts-ignore family: 4, - // Next.js cache - next: options.revalidate !== undefined - ? { revalidate: options.revalidate } - : undefined, + // Next.js cache with tags support + next: options.tags + ? { tags: options.tags } + : options.revalidate !== undefined + ? { revalidate: options.revalidate } + : undefined, }); } else if (fetchError?.cause?.code === "UND_ERR_CONNECT_TIMEOUT") { // Retry automatique sur timeout de connexion (cold start) @@ -175,10 +181,12 @@ export abstract class BaseApiService { connectTimeout: timeoutMs, bodyTimeout: timeoutMs, headersTimeout: timeoutMs, - // Next.js cache - next: options.revalidate !== undefined - ? { revalidate: options.revalidate } - : undefined, + // Next.js cache with tags support + next: options.tags + ? { tags: options.tags } + : options.revalidate !== undefined + ? { revalidate: options.revalidate } + : undefined, }); } else { throw fetchError; diff --git a/src/lib/services/home.service.ts b/src/lib/services/home.service.ts index 4382cbe..b8237ee 100644 --- a/src/lib/services/home.service.ts +++ b/src/lib/services/home.service.ts @@ -7,8 +7,12 @@ import { AppError } from "../../utils/errors"; export type { HomeData }; +// Cache tag pour invalidation ciblée +const HOME_CACHE_TAG = "home-data"; + export class HomeService extends BaseApiService { - private static readonly CACHE_TTL = 120; // 2 minutes + private static readonly CACHE_TTL = 120; // 2 minutes fallback + private static readonly CACHE_TAG = HOME_CACHE_TAG; static async getHomeData(): Promise { try { @@ -25,7 +29,7 @@ export class HomeService extends BaseApiService { }, }, {}, - { revalidate: this.CACHE_TTL } + { revalidate: this.CACHE_TTL, tags: [this.CACHE_TAG] } ), this.fetchFromApi>( { @@ -39,7 +43,7 @@ export class HomeService extends BaseApiService { }, }, {}, - { revalidate: this.CACHE_TTL } + { revalidate: this.CACHE_TTL, tags: [this.CACHE_TAG] } ), this.fetchFromApi>( { @@ -51,7 +55,7 @@ export class HomeService extends BaseApiService { }, }, {}, - { revalidate: this.CACHE_TTL } + { revalidate: this.CACHE_TTL, tags: [this.CACHE_TAG] } ), this.fetchFromApi>( { @@ -63,7 +67,7 @@ export class HomeService extends BaseApiService { }, }, {}, - { revalidate: this.CACHE_TTL } + { revalidate: this.CACHE_TTL, tags: [this.CACHE_TAG] } ), this.fetchFromApi>( { @@ -75,7 +79,7 @@ export class HomeService extends BaseApiService { }, }, {}, - { revalidate: this.CACHE_TTL } + { revalidate: this.CACHE_TTL, tags: [this.CACHE_TAG] } ), ]);