fix: invalidate HTML cache when files change on disk via mtime check (#281)#324
Conversation
|
Thank you for submitting a pull request. Please ensure your changes comply with the project's contribution guidelines and that all workflow checks pass successfully. Formatting and Branching
|
|
I appreciate the effort, but I don't think we should add runtime cache invalidation logic here. The stale cache only affects local development, since HTML files don't change after deployment in production. Rather than adding filesystem checks to every request, I'd prefer solving this at the development tooling level by updating the If you're interested, feel free to update this PR to make that change instead. |
Description
This PR fixes the
serveHtml()function inserver.jswhere HTML file contents were cached indefinitely on first read. Edits to any.htmlfile (e.g.,leaderboard.html,user.html) required a server restart to take effect — even during development withnodemon, which only watches.jschanges.The cache now stores each file's modification timestamp (
mtime) alongside its content. On every request,fs.statSync()checks whether the file has changed since it was cached. If modified, the file is re-read from disk and the cache is refreshed automatically.Linked Issue
Fixes #281
Changes Made
serveHtml()inserver.js— Before serving from cache,fs.statSync(filePath)retrieves the currentmtimeMs. The cache entry is only used ifcached.mtime >= stats.mtimeMs. On cache miss or stale timestamp, the file is re-read, and the cache is updated with{ mtime: stats.mtimeMs, data }.stringto{ mtime: number, data: string }to support invalidation.fs.statSync()throws (e.g., file deleted between requests), a500error is returned, matching the original behavior onfs.readFile()failure.Type of Change
Testing
node --check server.js)npx prettier --writepasses cleanlyChecklist
npx prettier --write .before submittingfeature/*branch, not themainbranch