Skip to content

Commit 6337a7b

Browse files
committed
Reduce chance of reload loop for HMR modules
1 parent 297c3cd commit 6337a7b

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

packages/app/src/sandbox/compile.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import parseConfigurations from '@codesandbox/common/lib/templates/configuration
55
import initializeErrorTransformers from 'sandbox-hooks/errors/transformers';
66
import { inject, unmount } from 'sandbox-hooks/react-error-overlay/overlay';
77
import { isBabel7 } from '@codesandbox/common/lib/utils/is-babel-7';
8-
import getDefinition, { TemplateType } from '@codesandbox/common/lib/templates/index';
8+
import getDefinition, {
9+
TemplateType,
10+
} from '@codesandbox/common/lib/templates/index';
911

1012
import getPreset from './eval';
1113
import Manager, { Manifest } from './eval/manager';
@@ -647,7 +649,7 @@ async function compile({
647649
manager.clearCache();
648650

649651
if (firstLoad && changedModuleCount === 0) {
650-
deleteAPICache(manager.id);
652+
await deleteAPICache(manager.id);
651653
}
652654
}
653655

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ function shouldSaveOnlineCache(firstRun: boolean, changes: number) {
3939
return false;
4040
}
4141

42+
export function clearIndexedDBCache() {
43+
return localforage.clear();
44+
}
45+
4246
export async function saveCache(
4347
sandboxId: string,
4448
managerModuleToTranspile: any,
@@ -64,7 +68,7 @@ export async function saveCache(
6468
debug(
6569
'Saving cache of ' +
6670
(JSON.stringify(managerState).length / 1024).toFixed(2) +
67-
'kb to localStorage'
71+
'kb to indexedDB'
6872
);
6973
}
7074
localforage.setItem(manager.id, managerState);
@@ -111,7 +115,7 @@ export async function saveCache(
111115
return Promise.resolve(false);
112116
}
113117

114-
export function deleteAPICache(sandboxId: string) {
118+
export function deleteAPICache(sandboxId: string): Promise<any> {
115119
if (APICacheUsed) {
116120
debug('Deleting cache of API');
117121
return window

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { flattenDeep, uniq, values } from 'lodash-es';
33
import { Protocol } from 'codesandbox-api';
44
import resolve from 'browser-resolve';
5-
import localforage from 'localforage';
65

76
import * as pathUtils from '@codesandbox/common/lib/utils/path';
87
import _debug from '@codesandbox/common/lib/utils/debug';
@@ -24,7 +23,7 @@ import TestRunner from './tests/jest-lite';
2423
import dependenciesToQuery from '../npm/dependencies-to-query';
2524
import { packageFilter } from './utils/resolve-utils';
2625

27-
import { ignoreNextCache, deleteAPICache } from './cache';
26+
import { ignoreNextCache, deleteAPICache, clearIndexedDBCache } from './cache';
2827
import { shouldTranspile } from './transpilers/babel/check';
2928
import { getGlobal } from '@codesandbox/common/lib/utils/global';
3029
import { ParsedConfigurationFiles } from '@codesandbox/common/lib/templates/template';
@@ -1165,12 +1164,12 @@ export default class Manager {
11651164

11661165
deleteAPICache() {
11671166
ignoreNextCache();
1168-
deleteAPICache(this.id);
1167+
return deleteAPICache(this.id);
11691168
}
11701169

11711170
clearCache() {
11721171
try {
1173-
localforage.clear();
1172+
clearIndexedDBCache();
11741173
} catch (ex) {
11751174
if (process.env.NODE_ENV === 'development') {
11761175
console.error(ex);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,12 @@ export default class TranspiledModule {
789789
// We're in a reload loop! Ignore all caches!
790790

791791
manager.clearCache();
792-
manager.deleteAPICache();
792+
manager.deleteAPICache().then(() => {
793+
document.location.reload();
794+
});
795+
} else {
796+
document.location.reload();
793797
}
794-
795-
location.reload();
796798
return {};
797799
}
798800
} else if (

0 commit comments

Comments
 (0)