Skip to content

Commit 8410c20

Browse files
authored
Fix dependency caching (codesandbox#598)
* Fix dependency caching * Add newManager check for loading cache
1 parent 9567afa commit 8410c20

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,19 @@ async function updateManager(
206206
if (!manager || manager.id !== sandboxId) {
207207
newManager = true;
208208
manager = new Manager(sandboxId, getPreset(template), managerModules);
209-
if (firstLoad) {
210-
// We save the state of transpiled modules, and load it here again. Gives
211-
// faster initial loads.
212-
213-
await manager.load();
214-
}
215209
}
216210

217211
if (isNewCombination || newManager) {
218212
manager.setManifest(manifest);
219213
}
214+
215+
if (firstLoad && newManager) {
216+
// We save the state of transpiled modules, and load it here again. Gives
217+
// faster initial loads.
218+
219+
await manager.load();
220+
}
221+
220222
manager.updateConfigurations(configurations);
221223
return await manager.updateData(managerModules);
222224
}

packages/app/src/sandbox/eval/manager.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import getDependencyName from './utils/get-dependency-name';
1818
import DependencyNotFoundError from '../errors/dependency-not-found-error';
1919
import ModuleNotFoundError from '../errors/module-not-found-error';
2020
import TestRunner from './tests/jest-lite';
21+
import dependenciesToQuery from '../npm/dependencies-to-query';
2122

2223
type Externals = {
2324
[name: string]: string,
@@ -682,6 +683,20 @@ export default class Manager {
682683
this.hardReload = true;
683684
}
684685

686+
getDependencyQuery() {
687+
if (!this.manifest || !this.manifest.dependencies) {
688+
return '';
689+
}
690+
691+
const normalizedDependencies = {};
692+
693+
this.manifest.dependencies.forEach(dep => {
694+
normalizedDependencies[dep.name] = dep.version;
695+
});
696+
697+
return dependenciesToQuery(normalizedDependencies);
698+
}
699+
685700
/**
686701
* Generate a JSON structure out of this manager that can be used to load
687702
* the manager later on. This is useful for faster initial loading.
@@ -697,11 +712,14 @@ export default class Manager {
697712
});
698713
});
699714

715+
const dependenciesQuery = this.getDependencyQuery();
716+
700717
await localforage.setItem(this.id, {
701718
transpiledModules: serializedTModules,
702719
cachedPaths: this.cachedPaths,
703720
version: VERSION,
704721
configurations: this.configurations,
722+
dependenciesQuery,
705723
});
706724
} catch (e) {
707725
if (process.env.NODE_ENV === 'development') {
@@ -721,16 +739,21 @@ export default class Manager {
721739
cachedPaths,
722740
version,
723741
configurations,
742+
dependenciesQuery,
724743
}: {
725744
transpiledModules: { [id: string]: SerializedTranspiledModule },
726745
cachedPaths: { [path: string]: string },
727746
version: string,
728747
configurations: Object,
748+
dependenciesQuery: string,
729749
} = data;
730750

731751
// Only use the cache if the cached version was cached with the same
732-
// version of the compiler
733-
if (version === VERSION) {
752+
// version of the compiler and dependencies haven't changed
753+
if (
754+
version === VERSION &&
755+
dependenciesQuery === this.getDependencyQuery()
756+
) {
734757
this.cachedPaths = cachedPaths;
735758
this.configurations = configurations;
736759

0 commit comments

Comments
 (0)