Skip to content

Commit 9bd4ff4

Browse files
fix: resolve type error and clean up duplicate code in og route
1 parent a3c53b4 commit 9bd4ff4

1 file changed

Lines changed: 5 additions & 15 deletions

File tree

app/api/og/route.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const displayDomain = (() => {
2121

2222
function getLuminance(hex: string) {
2323
let normalizedHex = hex.trim();
24-
// Normalize short hex (e.g., #fff or #ffff) to #rrggbb (alpha is ignored for luminance)
2524
if (normalizedHex.length === 4 || normalizedHex.length === 5) {
2625
normalizedHex = `#${normalizedHex[1]}${normalizedHex[1]}${normalizedHex[2]}${normalizedHex[2]}${normalizedHex[3]}${normalizedHex[3]}`;
2726
}
@@ -37,9 +36,6 @@ function getLuminance(hex: string) {
3736

3837
export async function GET(req: NextRequest) {
3938
const { searchParams } = new URL(req.url);
40-
const parsed = ogParamsSchema.parse(Object.fromEntries(searchParams.entries()));
41-
let { user } = parsed;
42-
const { theme, bg, text, accent } = parsed;
4339

4440
const parseResult = ogParamsSchema.safeParse(Object.fromEntries(searchParams.entries()));
4541

@@ -54,8 +50,6 @@ export async function GET(req: NextRequest) {
5450
}
5551

5652
const { user, theme, bg, text, accent, refresh } = parseResult.data;
57-
// Sanitize user: limit to 39 chars (GitHub max length) and strip invalid chars
58-
user = user.slice(0, 39).replace(/[^a-zA-Z0-9-]/g, '');
5953

6054
const selectedTheme = themes[theme] || themes.dark;
6155
const resolvedBg = `#${bg || selectedTheme.bg}`;
@@ -72,18 +66,17 @@ export async function GET(req: NextRequest) {
7266
let longestStreak = 0;
7367
let currentStreak = 0;
7468

75-
// Only the data fetching is wrapped in try/catch — not the JSX rendering.
7669
try {
77-
const calendar = await fetchGitHubContributions(user, { bypassCache: refresh });
78-
const userData = await fetchGitHubContributions(user, { bypassCache: true });
79-
const calendar = userData.calendar;
80-
const stats = calculateStreak(calendar);
70+
// bypassCache mirrors the ?refresh=true pattern used by /api/stats and /api/streak.
71+
// Without this, every link-preview bot crawl fires a fresh GitHub GraphQL request,
72+
// burning API quota on an endpoint that is embedded in every page's <meta> tag.
73+
const data = await fetchGitHubContributions(user, { bypassCache: refresh });
74+
const stats = calculateStreak(data.calendar ?? data);
8175
totalCommits = stats.totalContributions;
8276
longestStreak = stats.longestStreak;
8377
currentStreak = stats.currentStreak;
8478
} catch (err) {
8579
console.error('[OG] stats fetch failed:', err);
86-
// fallback to zeros if GitHub is unreachable
8780
}
8881

8982
const cacheControl = refresh
@@ -130,7 +123,6 @@ export async function GET(req: NextRequest) {
130123
{`@${user}`}
131124
</div>
132125
<div style={{ display: 'flex', gap: '48px' }}>
133-
{/* Total Commits */}
134126
<div
135127
style={{
136128
display: 'flex',
@@ -151,7 +143,6 @@ export async function GET(req: NextRequest) {
151143
Total Commits
152144
</div>
153145
</div>
154-
{/* Longest Streak */}
155146
<div
156147
style={{
157148
display: 'flex',
@@ -172,7 +163,6 @@ export async function GET(req: NextRequest) {
172163
{'Longest Streak 🔥'}
173164
</div>
174165
</div>
175-
{/* Current Streak */}
176166
<div
177167
style={{
178168
display: 'flex',

0 commit comments

Comments
 (0)