Skip to content

Commit 0d8e136

Browse files
Go back to old implementation
1 parent 57b19a8 commit 0d8e136

5 files changed

Lines changed: 83 additions & 63 deletions

File tree

astro.config.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import vercel from '@astrojs/vercel';
77

88
// https://astro.build/config
99
export default defineConfig({
10-
output: 'hybrid',
10+
output: 'static',
1111
adapter: vercel({
1212
imageService: true,
13-
devImageService: 'sharp',
1413
webAnalytics: {
1514
enabled: true
1615
}

src/components/EpisodeList.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useState } from 'preact/hooks';
33
import FormattedDate from '../components/FormattedDate';
44
import FullPlayButton from '../components/FullPlayButton';
55
import { currentEpisode } from '../components/state';
6-
import { getOptimizedImageUrl } from '../lib/image-utils';
76
import type { Episode } from '../lib/rss';
87

98
type Props = {
@@ -50,9 +49,12 @@ export default function EpisodeList({ episodes, url }: Props) {
5049
aria-hidden="true"
5150
class="mb-3 block h-20 w-20 rounded-md lg:mr-6"
5251
height={80}
53-
src={getOptimizedImageUrl(episode.episodeImage, 160, 160)}
52+
src={
53+
episode.episodeImage
54+
? `/api/image?src=${encodeURIComponent(episode.episodeImage)}`
55+
: '/images/www.png'
56+
}
5457
width={80}
55-
loading="lazy"
5658
/>
5759

5860
<div class="flex flex-col">

src/lib/image-utils.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/pages/api/image.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// src/pages/api/image.ts
2+
import sharp from 'sharp';
3+
import type { APIRoute } from 'astro';
4+
import fs from 'fs';
5+
import path from 'path';
6+
import crypto from 'crypto';
7+
8+
export const prerender = false;
9+
10+
const CACHE_DIR = './public/image-cache';
11+
12+
export const GET: APIRoute = async ({ url }) => {
13+
const searchParams = new URL(url).searchParams;
14+
const imageUrl = searchParams.get('src');
15+
16+
if (!imageUrl) {
17+
return new Response('Missing src parameter', { status: 400 });
18+
}
19+
20+
try {
21+
// Create cache key from URL
22+
const cacheKey = crypto.createHash('md5').update(imageUrl).digest('hex');
23+
const cachePath = path.join(CACHE_DIR, `${cacheKey}.avif`);
24+
25+
// Check if cached version exists
26+
if (fs.existsSync(cachePath)) {
27+
const cachedImage = fs.readFileSync(cachePath);
28+
return new Response(cachedImage, {
29+
headers: {
30+
'Content-Type': 'image/avif',
31+
'Cache-Control': 'public, max-age=31536000' // 1 year
32+
}
33+
});
34+
}
35+
36+
// Fetch original image
37+
const imageResponse = await fetch(imageUrl);
38+
if (!imageResponse.ok) {
39+
throw new Error(`Failed to fetch image: ${imageResponse.status}`);
40+
}
41+
42+
const imageBuffer = await imageResponse.arrayBuffer();
43+
44+
// Process with Sharp: resize to 160x160 and convert to avif
45+
const processedImage = await sharp(Buffer.from(imageBuffer))
46+
.resize(160, 160, {
47+
fit: 'cover',
48+
position: 'center'
49+
})
50+
.avif({ quality: 75 })
51+
.toBuffer();
52+
53+
// Cache the processed image
54+
fs.mkdirSync(CACHE_DIR, { recursive: true });
55+
fs.writeFileSync(cachePath, processedImage);
56+
57+
return new Response(processedImage, {
58+
headers: {
59+
'Content-Type': 'image/avif',
60+
'Cache-Control': 'public, max-age=31536000'
61+
}
62+
});
63+
} catch (error) {
64+
console.error('Image processing error:', error);
65+
66+
// Return your default image as fallback
67+
try {
68+
const fallbackPath = './public/images/www.png';
69+
const fallbackImage = fs.readFileSync(fallbackPath);
70+
return new Response(fallbackImage, {
71+
headers: { 'Content-Type': 'image/png' }
72+
});
73+
} catch {
74+
return new Response('Image not found', { status: 404 });
75+
}
76+
}
77+
};

vercel.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)