임동욱 임동욱 09-17
fix: 가맹몰 상세 화면이 열리지 않던 원인(이미지 두드리기 fetch 본문 취소가 끝나지 않음)을 node:http로 고친다
Next가 감싼 fetch는 response.body.cancel()이 영영 끝나지 않아(라우터 서버 안에서 재현) 추가
이미지 존재 확인이 멈추고 상세 화면이 렌더되지 않았다. node:http 요청으로 응답 머리만 보고
바로 끊으며 5초 타임아웃을 둔다(같은 프로세스에서 11장 0.4초 확인).

Co-Authored-By: Claude Opus 5 
@dd7c143f676b350f7aadd5da95aa931a862c02b4
lib/data/repositories/merchant-detail-repository.ts
--- lib/data/repositories/merchant-detail-repository.ts
+++ lib/data/repositories/merchant-detail-repository.ts
@@ -1,4 +1,6 @@
 import 'server-only';
+import { request as httpRequest } from 'node:http';
+import { request as httpsRequest } from 'node:https';
 import { getSessionAccessToken } from '@/lib/auth/dal';
 import { BackendRequestError, backendFetch } from '@/lib/http/backend-fetch';
 import { buildFileImageUrl } from '@/lib/data/repositories/file-repository';
@@ -141,30 +143,45 @@
   ];
 }
 
+const PROBE_TIMEOUT_MS = 5_000;
+
+/**
+ * 이미지 한 장이 있는지 — 응답 머리만 보고 바로 끊는다.
+ *
+ * `fetch`가 아니라 `node:http`를 쓴다. 백엔드 인증 예외가 GET에만 열려 HEAD는 401이고, Next가
+ * 감싼 `fetch`는 본문을 `cancel()`해도 끝나지 않아(확인함) 화면이 영영 뜨지 않는다.
+ */
+function probeImageUrl(url: string): Promise<boolean> {
+  return new Promise((resolve) => {
+    const target = new URL(url);
+    const request = (target.protocol === 'https:' ? httpsRequest : httpRequest)(
+      target,
+      { method: 'GET', timeout: PROBE_TIMEOUT_MS },
+      (response) => {
+        response.destroy();
+        resolve((response.statusCode ?? 500) < 400);
+      }
+    );
+    request.on('timeout', () => {
+      request.destroy();
+      resolve(false);
+    });
+    request.on('error', () => resolve(false));
+    request.end();
+  });
+}
+
 /**
  * 묶음 안의 이미지 수를 알려 주는 API가 없다(보고함). fileSn이 1부터 빈틈없이 붙으므로 최대 장수를
  * 한꺼번에 두드려 이어지는 앞부분만 취한다 — 차례로 두드리면 장수만큼 왕복이 늘어난다.
  * 없는 번호는 백엔드가 500으로 답한다.
- *
- * HEAD가 아니라 GET으로 두드린다 — 백엔드 인증 예외가 GET에만 열려 있어 HEAD는 401이다. 본문은
- * 상태만 보고 바로 끊는다.
  */
 async function probeImageUrls(fileId: string | null): Promise<string[]> {
   if (!fileId) return [];
   const candidates = Array.from({ length: MAX_EXTRA_IMAGES }, (_, index) =>
     buildFileImageUrl(fileId, index + 1)
   ).filter((url): url is string => url !== null);
-  const exists = await Promise.all(
-    candidates.map(async (url) => {
-      try {
-        const response = await fetch(url, { method: 'GET', cache: 'no-store' });
-        await response.body?.cancel().catch(() => undefined);
-        return response.ok;
-      } catch {
-        return false;
-      }
-    })
-  );
+  const exists = await Promise.all(candidates.map(probeImageUrl));
   const missingAt = exists.indexOf(false);
   return missingAt === -1 ? candidates : candidates.slice(0, missingAt);
 }
Add a comment
List