Skip to content

Commit b83104e

Browse files
committed
fix: 이미지 최적화 실패시 fallback 으로 원본이미지를 제공하도록 변경
1 parent 38d2afc commit b83104e

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

.vitepress/plugins/image-optimizer.ts

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,45 @@ async function convertImage(
5353
if (formats.includes("jpeg" as any) && [".jpg", ".jpeg"].includes(ext)) return;
5454
if (formats.includes("avif" as any) && ext === ".avif") return;
5555

56-
const image = sharp(sourcePath);
57-
58-
for (const format of formats) {
59-
const outputPath = join(dir, `${name}.${format}`);
60-
61-
// 이미 변환된 파일이 있으면 스킵
62-
if (existsSync(outputPath)) continue;
63-
64-
try {
65-
if (format === "webp") {
66-
await image
67-
.clone()
68-
.webp({ quality: options.webpQuality || 80 })
69-
.toFile(outputPath);
70-
console.log(`✅ WebP 생성: ${outputPath}`);
71-
} else if (format === "jpeg") {
72-
await image
73-
.clone()
74-
.jpeg({ quality: options.jpegQuality || 80 })
75-
.toFile(outputPath);
76-
console.log(`✅ JPEG 생성: ${outputPath}`);
77-
} else if (format === "avif") {
78-
await image
79-
.clone()
80-
.avif({ quality: options.avifQuality || 70 })
81-
.toFile(outputPath);
82-
console.log(`✅ AVIF 생성: ${outputPath}`);
56+
try {
57+
const image = sharp(sourcePath);
58+
59+
for (const format of formats) {
60+
const outputPath = join(dir, `${name}.${format}`);
61+
62+
// 이미 변환된 파일이 있으면 스킵
63+
if (existsSync(outputPath)) continue;
64+
65+
try {
66+
if (format === "webp") {
67+
await image
68+
.clone()
69+
.webp({ quality: options.webpQuality || 80 })
70+
.toFile(outputPath);
71+
console.log(`✅ WebP 생성: ${outputPath}`);
72+
} else if (format === "jpeg") {
73+
await image
74+
.clone()
75+
.jpeg({ quality: options.jpegQuality || 80 })
76+
.toFile(outputPath);
77+
console.log(`✅ JPEG 생성: ${outputPath}`);
78+
} else if (format === "avif") {
79+
await image
80+
.clone()
81+
.avif({ quality: options.avifQuality || 70 })
82+
.toFile(outputPath);
83+
console.log(`✅ AVIF 생성: ${outputPath}`);
84+
}
85+
} catch (error) {
86+
console.warn(
87+
`⚠️ ${format.toUpperCase()} 변환 실패 (${basename(sourcePath)}): ${error instanceof Error ? error.message : String(error)}`,
88+
);
8389
}
84-
} catch (error) {
85-
console.warn(`⚠️ 이미지 변환 실패 (${sourcePath}):`, error);
8690
}
91+
} catch (error) {
92+
console.warn(
93+
`⚠️ 이미지 로드 실패 (${basename(sourcePath)}): ${error instanceof Error ? error.message : String(error)}. 원본 파일을 사용합니다.`,
94+
);
8795
}
8896
}
8997

.vitepress/plugins/markdown-picture.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export function markdownPicturePlugin(md: MarkdownIt) {
4040
const srcWebp = changeExtension(src, "webp");
4141
const srcJpeg = changeExtension(src, "jpeg");
4242

43-
// picture 태그 생성
43+
// picture 태그 생성 (원본을 최종 fallback으로 사용)
4444
return `<picture>
4545
<source srcset="${srcWebp}" type="image/webp" />
4646
<source srcset="${srcJpeg}" type="image/jpeg" />
47-
<img src="${srcJpeg}" alt="${alt}" loading="lazy" />
47+
<img src="${src}" alt="${alt}" loading="lazy" />
4848
</picture>`;
4949
};
5050
}

0 commit comments

Comments
 (0)