Skip to content

Commit

Permalink
chore: fix redirect if not img request (#23)
Browse files Browse the repository at this point in the history
* chore: fix redirect if not img request

* chore: fix linter
  • Loading branch information
bxcodec committed Feb 12, 2024
1 parent 3576a98 commit a88680b
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions app/medium/[user]/[index]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@ export async function GET(req: Request) {
var pathName = reqURL.pathname.split('/');
const idx = pathName.pop(); // Get the last segment of the path
const username = pathName.pop(); // Get the username

// @ts-ignore
const { title, thumbnail, url, date, description } = await getArticle(idx, username);
const dest = headers.get('sec-fetch-dest') || headers.get('Sec-Fetch-Dest');
const accept = headers.get('accept');
// @ts-ignore
const isImage = dest ? dest === 'image' : !/text\/html/.test(accept);

if (isImage) {
// Generate the SVG content
const svgContent = medium({
title,
thumbnail,
url,
date,
description,
});

const svgContent = medium({
title,
thumbnail,
url,
date,
description,
});
return new Response(svgContent, {
headers: {
'Cache-Control': 's-maxage=3600, stale-while-revalidate',
'Content-Type': 'image/svg+xml',
},
});
}

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

0 comments on commit a88680b

Please sign in to comment.