Stencil dev server cacheDir stale cache invalidation strategy
0 reputation · 02 Jul 2021, 10:43 UTC
0 reputation · 02 Jul 2021, 10:43 UTC
The development server stores compiled component artifacts in a directory specified by the cacheDir option, defaulting to a temporary location. When source files change, the server does not automatically purge or refresh the cached entries, causing stale component outputs to persist across rebuilds. This behavior can lead to confusing debugging sessions and inconsistent UI rendering during development.
Currently there is no documented configuration to control cache invalidation timing or strategy. The only workaround is to manually delete the cache directory or restart the dev server after code changes.
To better understand and potentially improve this workflow, we need to clarify the following:
stencil build --watch regarding cacheDir updates?32275 reputation · 02 Jul 2021, 20:07 UTC
Stencil’s dev server does not expose a built‑in flag or environment variable that automatically clears cacheDir when source files change; cached artifacts persist until the server restarts or the directory is manually deleted.
devServer.cacheDir or devServer.cacheInvalidate triggers automatic purging on file change.stencil build --watch the server registers a chokidar watcher, rebuilds on each change, writes output to cacheDir (if caching is enabled), and does not remove existing entries.devServer.cache: false (or assigning an empty string to cacheDir) disables the persistent cache, forcing each rebuild to write to a temporary location and effectively achieving immediate invalidation.STENCIL_CACHE_DIR can override the cache directory; pointing it to a volatile OS‑temp folder that is cleared on each server start mimics invalidation, but the variable is read only at launch.fileChange event and call the internal cache‑clearing API (compiler.ctx.cache.clean()) to implement a user‑defined invalidation strategy.stencil.config.ts:
export const config: Config = {
devServer: {
cache: false // or cacheDir: ''
}
};
export const cacheInvalidationPlugin = {
name: 'cache-invalidation',
devServer: {
async fileChange(filePath, compilerCtx) {
// internal API – may change in future Stencil releases
if (compilerCtx?.cache?.clean) {
await compilerCtx.cache.clean();
}
}
}
};
and add it to the plugins array in stencil.config.ts.
Are you able to tolerate the increased rebuild latency that results from disabling devServer.cache? Your answer determines whether the simple cache: false approach is suitable or whether a plugin‑based clean‑up is preferable.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.