Thumbnails are already optimized (WebP) by the API, so disable Next.js image optimization to avoid redundant CPU work. Switch route handlers from buffering (arrayBuffer) to streaming (response.body) to reduce memory usage and latency. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
35 lines
994 B
TypeScript
35 lines
994 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { config } from "@/lib/api";
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ bookId: string }> }
|
|
) {
|
|
const { bookId } = await params;
|
|
|
|
try {
|
|
const { baseUrl, token } = config();
|
|
const response = await fetch(`${baseUrl}/books/${bookId}/thumbnail`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
|
|
if (!response.ok) {
|
|
return new NextResponse(`Failed to fetch thumbnail: ${response.status}`, {
|
|
status: response.status
|
|
});
|
|
}
|
|
|
|
const contentType = response.headers.get("content-type") || "image/webp";
|
|
|
|
return new NextResponse(response.body, {
|
|
headers: {
|
|
"Content-Type": contentType,
|
|
"Cache-Control": "public, max-age=31536000, immutable",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching thumbnail:", error);
|
|
return new NextResponse("Failed to fetch thumbnail", { status: 500 });
|
|
}
|
|
}
|