Pug cache continues to serve stale templates after file edits
0 reputation · 01 Dec 2023, 05:55 UTC
0 reputation · 01 Dec 2023, 05:55 UTC
When cache: true is enabled, Pug stores compiled templates in memory. If a template file is edited after caching, subsequent renders still use the old compiled function, producing stale content.
The documentation recommends clearing pug.cache or restarting the process, but it does not describe automated invalidation. In some releases compileDebug: true appears to disable caching, yet the interaction is not consistently documented.
In production, enabling cache improves performance but risks serving outdated data, especially in clustered deployments where each worker may hold its own cache copy.
What mechanisms does Pug currently expose for automatic cache invalidation on file changes? How does compileDebug influence caching behavior across major releases? In multi‑process setups, what patterns are recommended to synchronize cache clearing without downtime?
33275 reputation · 01 Dec 2023, 11:18 UTC
Pug’s internal cache does not watch the filesystem; when cache: true (or Express view cache) is enabled, a compiled function is stored in require('pug').cache keyed by the resolved file path and reused until the entry is removed or the Node process restarts. Editing a template file therefore yields stale output until the cache is cleared.
compileDebug interacts with cachingAcross all major releases (pre‑3.0 and 3.x), compileDebug: true only adds source‑map / line‑number instrumentation to the compiled function. It does **not** disable caching, nor does it change the key used in pug.cache. The flag is orthogonal: you can have both cache: true and compileDebug: true, and the debug‑enabled function will be cached just like a production build.
app.set('view cache', false) (Express) or pass {cache: false} to pug.renderFile..pug file changes:const chokidar = require('chokidar');
const pug = require('pug');
const path = require('path');
chokidar.watch('views/**/*.pug', { awaitWriteFinish: true })
.on('change', filePath => {
const resolved = path.resolve(filePath);
delete pug.cache[resolved];
console.log(`Cleared Pug cache for ${resolved}`);
});
After the watcher runs, the next renderFile call will recompile the edited template, eliminating stale output.
Each Node worker maintains its own pug.cache. To keep them in sync without downtime:
pm2 reload or a custom IPC channel) that triggers delete require('pug').cache[key] or require('pug').cache = {} in each worker before it starts accepting new traffic.layout.v12.pug). The new file gets a fresh cache key, so old workers can finish serving the previous version while new workers pick up the updated file.app.enable('view cache') and a Pug view that displays a timestamp.pm2 start app.js -i 2), hit each worker’s endpoint, edit a template, verify both workers serve stale content until you send a reload signal or restart the workers.If you are not using Express view cache but calling pug.renderFile directly, replace the app.set step with passing {cache: false} to the render call.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.