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
... | ... | @@ -1,4 +1,6 @@ |
| 1 | 1 |
import 'server-only'; |
| 2 |
+import { request as httpRequest } from 'node:http';
|
|
| 3 |
+import { request as httpsRequest } from 'node:https';
|
|
| 2 | 4 |
import { getSessionAccessToken } from '@/lib/auth/dal';
|
| 3 | 5 |
import { BackendRequestError, backendFetch } from '@/lib/http/backend-fetch';
|
| 4 | 6 |
import { buildFileImageUrl } from '@/lib/data/repositories/file-repository';
|
... | ... | @@ -141,30 +143,45 @@ |
| 141 | 143 |
]; |
| 142 | 144 |
} |
| 143 | 145 |
|
| 146 |
+const PROBE_TIMEOUT_MS = 5_000; |
|
| 147 |
+ |
|
| 148 |
+/** |
|
| 149 |
+ * 이미지 한 장이 있는지 — 응답 머리만 보고 바로 끊는다. |
|
| 150 |
+ * |
|
| 151 |
+ * `fetch`가 아니라 `node:http`를 쓴다. 백엔드 인증 예외가 GET에만 열려 HEAD는 401이고, Next가 |
|
| 152 |
+ * 감싼 `fetch`는 본문을 `cancel()`해도 끝나지 않아(확인함) 화면이 영영 뜨지 않는다. |
|
| 153 |
+ */ |
|
| 154 |
+function probeImageUrl(url: string): Promise<boolean> {
|
|
| 155 |
+ return new Promise((resolve) => {
|
|
| 156 |
+ const target = new URL(url); |
|
| 157 |
+ const request = (target.protocol === 'https:' ? httpsRequest : httpRequest)( |
|
| 158 |
+ target, |
|
| 159 |
+ { method: 'GET', timeout: PROBE_TIMEOUT_MS },
|
|
| 160 |
+ (response) => {
|
|
| 161 |
+ response.destroy(); |
|
| 162 |
+ resolve((response.statusCode ?? 500) < 400); |
|
| 163 |
+ } |
|
| 164 |
+ ); |
|
| 165 |
+ request.on('timeout', () => {
|
|
| 166 |
+ request.destroy(); |
|
| 167 |
+ resolve(false); |
|
| 168 |
+ }); |
|
| 169 |
+ request.on('error', () => resolve(false));
|
|
| 170 |
+ request.end(); |
|
| 171 |
+ }); |
|
| 172 |
+} |
|
| 173 |
+ |
|
| 144 | 174 |
/** |
| 145 | 175 |
* 묶음 안의 이미지 수를 알려 주는 API가 없다(보고함). fileSn이 1부터 빈틈없이 붙으므로 최대 장수를 |
| 146 | 176 |
* 한꺼번에 두드려 이어지는 앞부분만 취한다 — 차례로 두드리면 장수만큼 왕복이 늘어난다. |
| 147 | 177 |
* 없는 번호는 백엔드가 500으로 답한다. |
| 148 |
- * |
|
| 149 |
- * HEAD가 아니라 GET으로 두드린다 — 백엔드 인증 예외가 GET에만 열려 있어 HEAD는 401이다. 본문은 |
|
| 150 |
- * 상태만 보고 바로 끊는다. |
|
| 151 | 178 |
*/ |
| 152 | 179 |
async function probeImageUrls(fileId: string | null): Promise<string[]> {
|
| 153 | 180 |
if (!fileId) return []; |
| 154 | 181 |
const candidates = Array.from({ length: MAX_EXTRA_IMAGES }, (_, index) =>
|
| 155 | 182 |
buildFileImageUrl(fileId, index + 1) |
| 156 | 183 |
).filter((url): url is string => url !== null); |
| 157 |
- const exists = await Promise.all( |
|
| 158 |
- candidates.map(async (url) => {
|
|
| 159 |
- try {
|
|
| 160 |
- const response = await fetch(url, { method: 'GET', cache: 'no-store' });
|
|
| 161 |
- await response.body?.cancel().catch(() => undefined); |
|
| 162 |
- return response.ok; |
|
| 163 |
- } catch {
|
|
| 164 |
- return false; |
|
| 165 |
- } |
|
| 166 |
- }) |
|
| 167 |
- ); |
|
| 184 |
+ const exists = await Promise.all(candidates.map(probeImageUrl)); |
|
| 168 | 185 |
const missingAt = exists.indexOf(false); |
| 169 | 186 |
return missingAt === -1 ? candidates : candidates.slice(0, missingAt); |
| 170 | 187 |
} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?