Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix redirect if not img request #23

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}