Skip to content

Commit e2dd945

Browse files
authored
Improve performance of fetching metas (codesandbox#6343)
1 parent 70b8a7a commit e2dd945

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/sandpack-core/src/npm/dynamic/fetch-npm-module.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ function getMeta(
6969
packageJSONPath: string | null,
7070
version: string,
7171
useFallback = false
72-
): Promise<Meta> {
72+
): Promise<{ meta: Meta; fromCache: boolean }> {
7373
const [depName, depVersion] = resolveNPMAlias(name, version);
7474
const nameWithoutAlias = depName.replace(ALIAS_REGEX, '');
7575
const id = `${packageJSONPath || depName}@${depVersion}`;
7676
if (metas[id]) {
77-
return metas[id];
77+
return metas[id].then(x => ({
78+
meta: x,
79+
fromCache: true,
80+
}));
7881
}
7982

8083
const protocol = getFetchProtocol(depName, depVersion, useFallback);
@@ -85,7 +88,10 @@ function getMeta(
8588
throw e;
8689
});
8790

88-
return metas[id];
91+
return metas[id].then(x => ({
92+
meta: x,
93+
fromCache: false,
94+
}));
8995
}
9096

9197
export function downloadDependency(
@@ -339,7 +345,7 @@ export default async function fetchModule(
339345

340346
const { packageJSONPath, version } = versionInfo;
341347

342-
let meta: Meta;
348+
let meta: { meta: Meta; fromCache: boolean };
343349

344350
try {
345351
meta = await getMeta(dependencyName, packageJSONPath, version);
@@ -354,11 +360,11 @@ export default async function fetchModule(
354360
const normalizedCacheKey = dependencyName + rootPath;
355361

356362
const normalizedMeta =
357-
normalizedMetas[normalizedCacheKey] || prependRootPath(meta, rootPath);
363+
normalizedMetas[normalizedCacheKey] || prependRootPath(meta.meta, rootPath);
358364

359365
if (!normalizedMetas[normalizedCacheKey]) {
360366
normalizedMetas[normalizedCacheKey] = normalizedMeta;
361-
} else {
367+
} else if (!meta.fromCache) {
362368
combinedMetas = { ...combinedMetas, ...normalizedMeta };
363369
}
364370

0 commit comments

Comments
 (0)