Skip to content

Commit a88680b

Browse files
authored
chore: fix redirect if not img request (#23)
* chore: fix redirect if not img request * chore: fix linter
1 parent 3576a98 commit a88680b

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

app/medium/[user]/[index]/route.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,31 @@ export async function GET(req: Request) {
77
var pathName = reqURL.pathname.split('/');
88
const idx = pathName.pop(); // Get the last segment of the path
99
const username = pathName.pop(); // Get the username
10-
1110
// @ts-ignore
1211
const { title, thumbnail, url, date, description } = await getArticle(idx, username);
12+
const dest = headers.get('sec-fetch-dest') || headers.get('Sec-Fetch-Dest');
13+
const accept = headers.get('accept');
14+
// @ts-ignore
15+
const isImage = dest ? dest === 'image' : !/text\/html/.test(accept);
16+
17+
if (isImage) {
18+
// Generate the SVG content
19+
const svgContent = medium({
20+
title,
21+
thumbnail,
22+
url,
23+
date,
24+
description,
25+
});
1326

14-
const svgContent = medium({
15-
title,
16-
thumbnail,
17-
url,
18-
date,
19-
description,
20-
});
27+
return new Response(svgContent, {
28+
headers: {
29+
'Cache-Control': 's-maxage=3600, stale-while-revalidate',
30+
'Content-Type': 'image/svg+xml',
31+
},
32+
});
33+
}
2134

22-
return new Response(svgContent, {
23-
headers: {
24-
'Cache-Control': 's-maxage=3600, stale-while-revalidate',
25-
'Content-Type': 'image/svg+xml',
26-
},
27-
});
28-
}
35+
// Redirect to the URL if not an image request
36+
return Response.redirect(url, 301);
37+
}

0 commit comments

Comments
 (0)