Skip to content

Commit 3cb205c

Browse files
authored
feat: improve card loading speed (#2124)
* feat: improve card loading times This commit adds the `stale-while-revalidate` option to speed up the card loading times. * mend
1 parent f07cd13 commit 3cb205c

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

api/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ export default async (req, res) => {
6161
CONSTANTS.ONE_DAY,
6262
);
6363

64-
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
64+
res.setHeader(
65+
"Cache-Control",
66+
`max-age=${
67+
cacheSeconds / 2
68+
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
69+
);
6570

6671
return res.send(
6772
renderStatsCard(stats, {

api/pin.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ export default async (req, res) => {
5858
cacheSeconds = CONSTANTS.FOUR_HOURS;
5959
}
6060

61-
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
61+
res.setHeader(
62+
"Cache-Control",
63+
`max-age=${
64+
cacheSeconds / 2
65+
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
66+
);
6267

6368
return res.send(
6469
renderRepoCard(repoData, {

api/top-langs.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ export default async (req, res) => {
5252
CONSTANTS.ONE_DAY,
5353
);
5454

55-
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
55+
res.setHeader(
56+
"Cache-Control",
57+
`max-age=${
58+
cacheSeconds / 2
59+
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
60+
);
5661

5762
return res.send(
5863
renderTopLanguages(topLangs, {

api/wakatime.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ export default async (req, res) => {
5252
cacheSeconds = CONSTANTS.FOUR_HOURS;
5353
}
5454

55-
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
55+
res.setHeader(
56+
"Cache-Control",
57+
`max-age=${
58+
cacheSeconds / 2
59+
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
60+
);
5661

5762
return res.send(
5863
renderWakatimeCard(stats, {

tests/api.test.js

+30-5
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,12 @@ describe("Test /api/", () => {
160160

161161
expect(res.setHeader.mock.calls).toEqual([
162162
["Content-Type", "image/svg+xml"],
163-
["Cache-Control", `public, max-age=${CONSTANTS.FOUR_HOURS}`],
163+
[
164+
"Cache-Control",
165+
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
166+
CONSTANTS.FOUR_HOURS
167+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
168+
],
164169
]);
165170
});
166171

@@ -170,7 +175,12 @@ describe("Test /api/", () => {
170175

171176
expect(res.setHeader.mock.calls).toEqual([
172177
["Content-Type", "image/svg+xml"],
173-
["Cache-Control", `public, max-age=${15000}`],
178+
[
179+
"Cache-Control",
180+
`max-age=7500, s-maxage=${15000}, stale-while-revalidate=${
181+
CONSTANTS.ONE_DAY
182+
}`,
183+
],
174184
]);
175185
});
176186

@@ -191,7 +201,12 @@ describe("Test /api/", () => {
191201

192202
expect(res.setHeader.mock.calls).toEqual([
193203
["Content-Type", "image/svg+xml"],
194-
["Cache-Control", `public, max-age=${CONSTANTS.ONE_DAY}`],
204+
[
205+
"Cache-Control",
206+
`max-age=${CONSTANTS.ONE_DAY / 2}, s-maxage=${
207+
CONSTANTS.ONE_DAY
208+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
209+
],
195210
]);
196211
}
197212

@@ -202,7 +217,12 @@ describe("Test /api/", () => {
202217

203218
expect(res.setHeader.mock.calls).toEqual([
204219
["Content-Type", "image/svg+xml"],
205-
["Cache-Control", `public, max-age=${CONSTANTS.FOUR_HOURS}`],
220+
[
221+
"Cache-Control",
222+
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
223+
CONSTANTS.FOUR_HOURS
224+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
225+
],
206226
]);
207227
}
208228

@@ -212,7 +232,12 @@ describe("Test /api/", () => {
212232

213233
expect(res.setHeader.mock.calls).toEqual([
214234
["Content-Type", "image/svg+xml"],
215-
["Cache-Control", `public, max-age=${CONSTANTS.FOUR_HOURS}`],
235+
[
236+
"Cache-Control",
237+
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
238+
CONSTANTS.FOUR_HOURS
239+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
240+
],
216241
]);
217242
}
218243
});

0 commit comments

Comments
 (0)