Skip to content

Commit

Permalink
feat: improve card loading speed (#2124)
Browse files Browse the repository at this point in the history
* feat: improve card loading times

This commit adds the `stale-while-revalidate` option to speed up the
card loading times.

* mend
  • Loading branch information
rickstaa committed Nov 21, 2022
1 parent f07cd13 commit 3cb205c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
7 changes: 6 additions & 1 deletion api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ export default async (req, res) => {
CONSTANTS.ONE_DAY,
);

res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
res.setHeader(
"Cache-Control",
`max-age=${
cacheSeconds / 2
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);

return res.send(
renderStatsCard(stats, {
Expand Down
7 changes: 6 additions & 1 deletion api/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ export default async (req, res) => {
cacheSeconds = CONSTANTS.FOUR_HOURS;
}

res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
res.setHeader(
"Cache-Control",
`max-age=${
cacheSeconds / 2
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);

return res.send(
renderRepoCard(repoData, {
Expand Down
7 changes: 6 additions & 1 deletion api/top-langs.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export default async (req, res) => {
CONSTANTS.ONE_DAY,
);

res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
res.setHeader(
"Cache-Control",
`max-age=${
cacheSeconds / 2
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);

return res.send(
renderTopLanguages(topLangs, {
Expand Down
7 changes: 6 additions & 1 deletion api/wakatime.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export default async (req, res) => {
cacheSeconds = CONSTANTS.FOUR_HOURS;
}

res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
res.setHeader(
"Cache-Control",
`max-age=${
cacheSeconds / 2
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);

return res.send(
renderWakatimeCard(stats, {
Expand Down
35 changes: 30 additions & 5 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ describe("Test /api/", () => {

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
["Cache-Control", `public, max-age=${CONSTANTS.FOUR_HOURS}`],
[
"Cache-Control",
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
CONSTANTS.FOUR_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
});

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

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
["Cache-Control", `public, max-age=${15000}`],
[
"Cache-Control",
`max-age=7500, s-maxage=${15000}, stale-while-revalidate=${
CONSTANTS.ONE_DAY
}`,
],
]);
});

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

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
["Cache-Control", `public, max-age=${CONSTANTS.ONE_DAY}`],
[
"Cache-Control",
`max-age=${CONSTANTS.ONE_DAY / 2}, s-maxage=${
CONSTANTS.ONE_DAY
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}

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

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
["Cache-Control", `public, max-age=${CONSTANTS.FOUR_HOURS}`],
[
"Cache-Control",
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
CONSTANTS.FOUR_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}

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

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
["Cache-Control", `public, max-age=${CONSTANTS.FOUR_HOURS}`],
[
"Cache-Control",
`max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${
CONSTANTS.FOUR_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}
});
Expand Down

1 comment on commit 3cb205c

@vercel
Copy link

@vercel vercel bot commented on 3cb205c Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.