feat: persistance des filtres server-side via cookies
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 47s
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 47s
Remplace localStorage par des cookies pour la persistance des filtres. Le proxy restaure les filtres sauvegardés côté serveur, éliminant le flash au chargement. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,8 +8,16 @@ function getSecret(): Uint8Array {
|
||||
return new TextEncoder().encode(secret);
|
||||
}
|
||||
|
||||
/** Paths where filter persistence is active */
|
||||
const FILTER_PATHS = ["/series", "/books", "/authors"];
|
||||
|
||||
/** Convert a basePath to a cookie name: /series → filters_series */
|
||||
function filterCookieName(basePath: string): string {
|
||||
return `filters_${basePath.replace(/^\//, "").replace(/\//g, "_")}`;
|
||||
}
|
||||
|
||||
export async function proxy(req: NextRequest) {
|
||||
const { pathname } = req.nextUrl;
|
||||
const { pathname, searchParams } = req.nextUrl;
|
||||
|
||||
// Skip auth for login page and auth API routes
|
||||
if (pathname.startsWith("/login") || pathname.startsWith("/api/auth")) {
|
||||
@@ -20,10 +28,38 @@ export async function proxy(req: NextRequest) {
|
||||
if (token) {
|
||||
try {
|
||||
await jwtVerify(token, getSecret());
|
||||
return NextResponse.next();
|
||||
} catch {
|
||||
// Token invalid or expired
|
||||
// Token invalid or expired — redirect to login
|
||||
const loginUrl = new URL("/login", req.url);
|
||||
loginUrl.searchParams.set("from", pathname);
|
||||
return NextResponse.redirect(loginUrl);
|
||||
}
|
||||
|
||||
// Restore saved filters from cookie for filter pages
|
||||
if (FILTER_PATHS.includes(pathname)) {
|
||||
const nonPaginationParams = Array.from(searchParams.entries()).filter(
|
||||
([key]) => key !== "page" && key !== "limit"
|
||||
);
|
||||
if (nonPaginationParams.length === 0) {
|
||||
const cookieName = filterCookieName(pathname);
|
||||
const cookie = req.cookies.get(cookieName);
|
||||
if (cookie?.value) {
|
||||
try {
|
||||
const filters: Record<string, string> = JSON.parse(cookie.value);
|
||||
const entries = Object.entries(filters).filter(([, v]) => v && v.trim());
|
||||
if (entries.length > 0) {
|
||||
const url = req.nextUrl.clone();
|
||||
for (const [key, value] of entries) {
|
||||
url.searchParams.set(key, value);
|
||||
}
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const loginUrl = new URL("/login", req.url);
|
||||
|
||||
Reference in New Issue
Block a user