Skip to content

Commit 9783d96

Browse files
committed
Add cache deletion
1 parent 1c84e73 commit 9783d96

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from './boilerplates';
2323

2424
import loadDependencies from './npm';
25-
import { consumeCache, saveCache } from './eval/cache';
25+
import { consumeCache, saveCache, deleteAPICache } from './eval/cache';
2626
import getDefinition from '../../../common/templates/index';
2727

2828
let initializedResizeListener = false;
@@ -41,6 +41,7 @@ export function getCurrentManager(): ?Manager {
4141

4242
let firstLoad = true;
4343
let hadError = false;
44+
let changedModuleCount = 0;
4445

4546
const DEPENDENCY_ALIASES = {
4647
'@vue/cli-plugin-babel': '@vue/babel-preset-app',
@@ -251,7 +252,9 @@ async function updateManager(
251252
}
252253

253254
manager.updateConfigurations(configurations);
254-
return manager.updateData(managerModules);
255+
return manager.updateData(managerModules).then(x => {
256+
changedModuleCount = x.length;
257+
});
255258
}
256259

257260
function initializeResizeListener() {
@@ -535,13 +538,23 @@ async function compile({
535538
type: 'success',
536539
});
537540

538-
saveCache(sandboxId, managerModuleToTranspile, manager, firstLoad);
541+
saveCache(
542+
sandboxId,
543+
managerModuleToTranspile,
544+
manager,
545+
changedModuleCount,
546+
firstLoad
547+
);
539548
} catch (e) {
540549
console.log('Error in sandbox:');
541550
console.error(e);
542551

543552
if (manager) {
544553
manager.clearCache();
554+
555+
if (firstLoad && changedModuleCount === 0) {
556+
deleteAPICache(manager.id);
557+
}
545558
}
546559

547560
if (firstLoad) {

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const debug = _debug('cs:compiler:cache');
99

1010
const host = process.env.CODESANDBOX_HOST;
1111

12+
let APICacheUsed = false;
13+
1214
try {
1315
localforage.config({
1416
name: 'CodeSandboxApp',
@@ -24,8 +26,8 @@ try {
2426
console.warn(e);
2527
}
2628

27-
function shouldSaveOnlineCache(firstRun: boolean) {
28-
if (!firstRun) {
29+
function shouldSaveOnlineCache(firstRun: boolean, changes: number) {
30+
if (!firstRun || changes > 0) {
2931
return false;
3032
}
3133

@@ -40,6 +42,7 @@ export async function saveCache(
4042
sandboxId: string,
4143
managerModuleToTranspile: any,
4244
manager: Manager,
45+
changes: number,
4346
firstRun: boolean
4447
) {
4548
const managerState = {
@@ -65,7 +68,7 @@ export async function saveCache(
6568
manager.clearCache();
6669
}
6770

68-
if (shouldSaveOnlineCache(firstRun)) {
71+
if (shouldSaveOnlineCache(firstRun, changes)) {
6972
const stringifiedManagerState = JSON.stringify(managerState);
7073

7174
debug(
@@ -95,6 +98,29 @@ export async function saveCache(
9598
return Promise.resolve(false);
9699
}
97100

101+
export function deleteAPICache(sandboxId: string) {
102+
if (APICacheUsed) {
103+
debug('Deleting cache of API');
104+
return window
105+
.fetch(`${host}/api/v1/sandboxes/${sandboxId}/cache`, {
106+
method: 'DELETE',
107+
body: JSON.stringify({
108+
version: SCRIPT_VERSION,
109+
}),
110+
headers: {
111+
'Content-Type': 'application/json',
112+
},
113+
})
114+
.then(x => x.json())
115+
.catch(e => {
116+
console.error('Something went wrong while deleting cache.');
117+
console.error(e);
118+
});
119+
}
120+
121+
return Promise.resolve(false);
122+
}
123+
98124
function findCacheToUse(cache1, cache2) {
99125
if (!cache1 && !cache2) {
100126
return null;
@@ -136,6 +162,12 @@ export async function consumeCache(manager: Manager) {
136162
const version = SCRIPT_VERSION;
137163

138164
if (cache.version === version) {
165+
if (cache === localData) {
166+
APICacheUsed = false;
167+
} else {
168+
APICacheUsed = true;
169+
}
170+
139171
debug(
140172
`Loading cache from ${cache === localData ? 'localStorage' : 'API'}`,
141173
cache

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import TestRunner from './tests/jest-lite';
2323
import dependenciesToQuery from '../npm/dependencies-to-query';
2424
import isESModule from './utils/is-es-module';
2525

26-
import { ignoreNextCache } from './cache';
26+
import { ignoreNextCache, deleteAPICache } from './cache';
2727

2828
type Externals = {
2929
[name: string]: string,
@@ -829,9 +829,13 @@ export default class Manager {
829829
}
830830
}
831831

832+
deleteAPICache() {
833+
ignoreNextCache();
834+
deleteAPICache(this.id);
835+
}
836+
832837
clearCache() {
833838
try {
834-
ignoreNextCache();
835839
localforage.clear();
836840
} catch (ex) {
837841
if (process.env.NODE_ENV === 'development') {

packages/app/src/sandbox/eval/transpiled-module.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ export default class TranspiledModule {
657657
// We're in a reload loop! Ignore all caches!
658658

659659
manager.clearCache();
660+
manager.deleteAPICache();
660661
}
661662

662663
location.reload();

0 commit comments

Comments
 (0)