Skip to content

🎥 Make videos programmatically with React

clearly signal cache chance

Jonny Burger0ab319b03f0a
diff --git a/packages/bundler/src/webpack-cache.ts b/packages/bundler/src/webpack-cache.ts
index ad34d10ca1..c469053afc 100644
--- a/packages/bundler/src/webpack-cache.ts
+++ b/packages/bundler/src/webpack-cache.ts
@@ -3,6 +3,8 @@ import path from 'path';
type Environment = 'development' | 'production';
+type CacheState = 'exists' | 'other-exists' | 'does-not-exist';
+
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
@@ -56,25 +58,54 @@ const remotionCacheLocation = (environment: Environment, hash: string) => {
);
};
-export const clearCache = (environment: Environment, hash: string) => {
- return (fs.promises.rm ?? fs.promises.rmdir)(
- remotionCacheLocation(environment, hash),
- {
- recursive: true,
- }
- );
+export const clearCache = () => {
+ return (fs.promises.rm ?? fs.promises.rmdir)(getWebpackCacheDir(), {
+ recursive: true,
+ });
+};
+
+const getPrefix = (environment: Environment) => {
+ return `remotion-v4-${environment}`;
};
export const getWebpackCacheName = (environment: Environment, hash: string) => {
if (environment === 'development') {
- return `remotion-v3-${environment}-${hash}`;
+ return `${getPrefix(environment)}-${hash}`;
}
// In production, the cache is independent from input props because
// they are passed over URL params. Speed is mostly important in production.
- return `remotion-v3-${environment}-${hash}`;
+ return `${getPrefix(environment)}-${hash}`;
};
-export const cacheExists = (environment: Environment, hash: string) => {
- return fs.existsSync(remotionCacheLocation(environment, hash));
+const hasOtherCache = (environment: Environment) => {
+ const cacheDir = fs.readdirSync(getWebpackCacheDir());
+ if (
+ cacheDir.find((c) => {
+ return c.startsWith(getPrefix(environment));
+ })
+ ) {
+ return true;
+ }
+
+ return false;
+};
+
+export const cacheExists = (
+ environment: Environment,
+ hash: string
+): CacheState => {
+ if (fs.existsSync(remotionCacheLocation(environment, hash))) {
+ return 'exists';
+ }
+
+ if (!fs.existsSync(getWebpackCacheDir())) {
+ return 'does-not-exist';
+ }
+
+ if (hasOtherCache(environment)) {
+ return 'other-exists';
+ }
+
+ return 'does-not-exist';
};
diff --git a/packages/cli/src/setup-cache.ts b/packages/cli/src/setup-cache.ts
index d09d0005d6..5885f257f7 100644
--- a/packages/cli/src/setup-cache.ts
+++ b/packages/cli/src/setup-cache.ts
@@ -36,9 +36,14 @@ export const bundleOnCli = async ({
const [hash] = BundlerInternals.getConfig('', fullPath, onProgress, options);
const cacheExistedBefore = BundlerInternals.cacheExists('production', hash);
- if (cacheExistedBefore && !shouldCache) {
+ if (cacheExistedBefore !== 'does-not-exist' && !shouldCache) {
Log.info('🧹 Cache disabled but found. Deleting... ');
- await BundlerInternals.clearCache('production', hash);
+ await BundlerInternals.clearCache();
+ }
+
+ if (cacheExistedBefore === 'other-exists' && shouldCache) {
+ Log.info('🧹 Webpack config change detected. Clearing cache... ');
+ await BundlerInternals.clearCache();
}
const bundleStartTime = Date.now();
@@ -65,9 +70,16 @@ export const bundleOnCli = async ({
}) + '\n'
);
Log.verbose('Bundled under', bundled);
- const cacheExistedAfter = BundlerInternals.cacheExists('production', hash);
- if (cacheExistedAfter && !cacheExistedBefore) {
- Log.info('⚡️ Cached bundle. Subsequent renders will be faster.');
+ const cacheExistedAfter =
+ BundlerInternals.cacheExists('production', hash) === 'exists';
+
+ if (cacheExistedAfter) {
+ if (
+ cacheExistedBefore === 'does-not-exist' ||
+ cacheExistedBefore === 'other-exists'
+ ) {
+ Log.info('⚡️ Cached bundle. Subsequent renders will be faster.');
+ }
}
return bundled;