fix: Add HEAD method support and fix proxy URL in Next.js assets route
- Add HEAD method handler in Next.js route - Fix proxy URL to use correct city-service endpoint - Handle HEAD requests properly (return headers only) - This should fix 405 errors when browser checks image availability
This commit is contained in:
@@ -14,6 +14,20 @@ const ASSETS_BUCKET = process.env.ASSETS_BUCKET || 'daarion-assets';
|
|||||||
export async function GET(
|
export async function GET(
|
||||||
request: NextRequest,
|
request: NextRequest,
|
||||||
{ params }: { params: Promise<{ path: string[] }> }
|
{ params }: { params: Promise<{ path: string[] }> }
|
||||||
|
) {
|
||||||
|
return handleAssetRequest(request, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function HEAD(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: Promise<{ path: string[] }> }
|
||||||
|
) {
|
||||||
|
return handleAssetRequest(request, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleAssetRequest(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: Promise<{ path: string[] }> }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const { path } = await params;
|
const { path } = await params;
|
||||||
@@ -24,15 +38,18 @@ export async function GET(
|
|||||||
// Should proxy to: http://minio:9000/daarion-assets/microdao/logo/2025/12/02/abc123.png
|
// Should proxy to: http://minio:9000/daarion-assets/microdao/logo/2025/12/02/abc123.png
|
||||||
const minioUrl = `${MINIO_ENDPOINT}/${ASSETS_BUCKET}/${objectPath}`;
|
const minioUrl = `${MINIO_ENDPOINT}/${ASSETS_BUCKET}/${objectPath}`;
|
||||||
|
|
||||||
// For Docker network, use service name
|
// Proxy through city-service
|
||||||
// For external access, we need to proxy through city-service or use internal network
|
// INTERNAL_API_URL is like http://daarion-city-service:7001
|
||||||
const proxyUrl = process.env.INTERNAL_API_URL
|
// We need: http://daarion-city-service:7001/city/assets/proxy/...
|
||||||
? `${process.env.INTERNAL_API_URL.replace('/city', '')}/assets/proxy/${objectPath}`
|
const cityServiceUrl = process.env.INTERNAL_API_URL || 'http://daarion-city-service:7001';
|
||||||
: minioUrl;
|
const proxyUrl = `${cityServiceUrl}/city/assets/proxy/${objectPath}`;
|
||||||
|
|
||||||
// Try to fetch from MinIO
|
// Use the same HTTP method as the incoming request (GET or HEAD)
|
||||||
|
const method = request.method as 'GET' | 'HEAD';
|
||||||
|
|
||||||
|
// Try to fetch from city-service proxy
|
||||||
const response = await fetch(proxyUrl, {
|
const response = await fetch(proxyUrl, {
|
||||||
method: 'GET',
|
method: method,
|
||||||
headers: {
|
headers: {
|
||||||
'Accept': request.headers.get('Accept') || '*/*',
|
'Accept': request.headers.get('Accept') || '*/*',
|
||||||
},
|
},
|
||||||
@@ -46,7 +63,20 @@ export async function GET(
|
|||||||
const contentType = response.headers.get('Content-Type') || 'application/octet-stream';
|
const contentType = response.headers.get('Content-Type') || 'application/octet-stream';
|
||||||
const contentLength = response.headers.get('Content-Length');
|
const contentLength = response.headers.get('Content-Length');
|
||||||
|
|
||||||
// Stream the response
|
// For HEAD requests, return headers only
|
||||||
|
if (method === 'HEAD') {
|
||||||
|
return new NextResponse(null, {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': contentType,
|
||||||
|
'Content-Length': contentLength || '0',
|
||||||
|
'Cache-Control': 'public, max-age=86400, immutable',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// For GET requests, return file data
|
||||||
const blob = await response.blob();
|
const blob = await response.blob();
|
||||||
const buffer = await blob.arrayBuffer();
|
const buffer = await blob.arrayBuffer();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user