Skip to content

Commit e9fc10c

Browse files
committed
Improve caching for versionData
1 parent c0be266 commit e9fc10c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/lib/api/npm/getVersionData.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cacheLife } from "next/cache";
2+
import { createSimplePackageSpec } from "^/lib/createSimplePackageSpec";
23
import type SimplePackageSpec from "^/lib/SimplePackageSpec";
3-
import { simplePackageSpecToString } from "^/lib/SimplePackageSpec";
44
import packument from "./packument";
55

66
// Packuments include a lot of data, often enough to make them too large for the cache.
@@ -16,21 +16,21 @@ export type VersionMap = {
1616
[version: string]: VersionData;
1717
};
1818

19-
async function getVersionData(
20-
spec: string | SimplePackageSpec,
21-
): Promise<VersionMap> {
19+
/**
20+
* Separate function that takes only packagename for better caching.
21+
*
22+
* We want `a@1.2.3` and `a@2.0.0` to share the same cache entry for `a`.
23+
*/
24+
async function getVersionMap(packageName: string): Promise<VersionMap> {
2225
"use cache";
2326

2427
cacheLife("hours");
2528

26-
const specString =
27-
typeof spec === "string" ? spec : simplePackageSpecToString(spec);
28-
2929
const {
3030
time,
3131
"dist-tags": tags,
3232
versions,
33-
} = await packument(specString, {
33+
} = await packument(packageName, {
3434
// Response is too large to cache in Next's Data Cache; always fetch
3535
cache: "no-store",
3636
});
@@ -55,4 +55,13 @@ async function getVersionData(
5555
return versionData;
5656
}
5757

58+
async function getVersionData(
59+
spec: string | SimplePackageSpec,
60+
): Promise<VersionMap> {
61+
const { name } =
62+
typeof spec === "string" ? createSimplePackageSpec(spec) : spec;
63+
64+
return getVersionMap(name);
65+
}
66+
5867
export default getVersionData;

0 commit comments

Comments
 (0)