Skip to content

Commit c63f7a2

Browse files
authored
fix: use immutable cache for historical year queries (JhaSourav07#2108)
## Description Fixes JhaSourav07#2101 When using the `?year=` parameter to query a past year (e.g. `?year=2023`), the API was caching responses with a midnight-expiring TTL — same as live queries. Since historical year data is immutable, this caused unnecessary CDN re-fetching every 24 hours. ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview **Historical year (`?year=2023`):** cache-control: public, s-maxage=31536000, immutable **Current year (no year param):** cache-control: public, s-maxage=40362, stale-while-revalidate=86400 ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [ ] I have run `npm run test` and all tests pass locally. - [ ] I have run `npm run test:coverage` and branch coverage is at or above 70%. - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [ ] I have started the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [ ] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [X] (Recommended) I joined the CommitPulse Discord server for faster collaboration, mentorship, and PR support.
2 parents 09ffa49 + c988f03 commit c63f7a2

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

app/api/streak/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ export async function GET(request: Request) {
106106
: year
107107
? `${year}-12-31T23:59:59Z`
108108
: undefined;
109+
const currentYear = new Date().getUTCFullYear();
110+
const isHistoricalYear = !!year && Number(year) < currentYear;
109111

110112
let timezone = 'UTC';
111113
if (tzParam) {
@@ -224,7 +226,9 @@ export async function GET(request: Request) {
224226
: getSecondsUntilUTCMidnight();
225227
const cacheControl = refresh
226228
? 'no-cache, no-store, must-revalidate'
227-
: `public, s-maxage=${secondsToMidnight}, stale-while-revalidate=86400`;
229+
: isHistoricalYear
230+
? 'public, s-maxage=31536000, immutable'
231+
: `public, s-maxage=${secondsToMidnight}, stale-while-revalidate=86400`;
228232

229233
return new NextResponse(svg, {
230234
headers: {

0 commit comments

Comments
 (0)