Skip to content

fix: invalidate HTML cache when files change on disk via mtime check (#281)#324

Open
rishab11250 wants to merge 1 commit into
codepvg:mainfrom
rishab11250:feature/html-cache-mtime-validation-issue281
Open

fix: invalidate HTML cache when files change on disk via mtime check (#281)#324
rishab11250 wants to merge 1 commit into
codepvg:mainfrom
rishab11250:feature/html-cache-mtime-validation-issue281

Conversation

@rishab11250

Copy link
Copy Markdown
Contributor

Description

This PR fixes the serveHtml() function in server.js where HTML file contents were cached indefinitely on first read. Edits to any .html file (e.g., leaderboard.html, user.html) required a server restart to take effect — even during development with nodemon, which only watches .js changes.

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

  • Modified serveHtml() in server.js — Before serving from cache, fs.statSync(filePath) retrieves the current mtimeMs. The cache entry is only used if cached.mtime >= stats.mtimeMs. On cache miss or stale timestamp, the file is re-read, and the cache is updated with { mtime: stats.mtimeMs, data }.
  • Cache structure changed from string to { mtime: number, data: string } to support invalidation.
  • Error handling preserved — if fs.statSync() throws (e.g., file deleted between requests), a 500 error is returned, matching the original behavior on fs.readFile() failure.

Type of Change

  • Bug fix
  • New feature
  • UI/Visual update
  • Documentation update
  • Refactor

Testing

  • Tested locally (syntax validation with node --check server.js)
  • Server runtime startup validated ("Server running at http://localhost:3000")
  • No console errors introduced
  • npx prettier --write passes cleanly

Checklist

  • My code follows the project's coding style
  • I have formatted my code locally by running npx prettier --write . before submitting
  • I am submitting my PR from a dedicated feature/* branch, not the main branch
  • I have performed a self-review of my code
  • My changes generate no new warnings or errors
  • I have updated documentation if required
  • I have linked the relevant issue

@github-actions

Copy link
Copy Markdown
Contributor

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

  • Please confirm you have formatted your code locally using npx prettier --write . before requesting a review.
  • Ensure this PR is made from a feature/* branch and not main.

Note: This project is currently maintained by a solo maintainer, so reviews and responses may sometimes take a little time. Thanks for your patience.

@jagdish-15

Copy link
Copy Markdown
Collaborator

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 dev script to watch HTML/CSS files as discussed in the issue.

If you're interested, feel free to update this PR to make that change instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

server.js HTML cache never invalidates when files change on disk

2 participants