Next.js (app dir) returns unexpected value for last modified date of data files at page request time

My Next.js site dynamically renders content files server-side on demand (ie picture a blog).

Im trying to obtain the last modified date of these data files in the filesystem by doing fs.statFIle() and stats.mtime.toLocaleDateString().This works as expected locally, in both dev and production. On the server, at build time the stats also come back as expected - but i can’t pre-renderer these pages because: reasons! :slight_smile: When i publish the site however, all the file stats are coming back at page request time as

{
  ...
  "atimeMs": 315532800000,
  "mtimeMs": 315532800000,
  "ctimeMs": 315532800000,
  "birthtimeMs": 0,
  "atime": "1980-01-01T00:00:00.000Z",
  "mtime": "1980-01-01T00:00:00.000Z",
  "ctime": "1980-01-01T00:00:00.000Z",
  "birthtime": "1970-01-01T00:00:00.000Z"
}

The file size is right, so it isn’t failing, but the dates are getting mangled somehow. As far as I can tell this is all happening on the server ,so i don’t think it’s the data objects failing to serialise between client and server? But I’m stumped. Im sure this is my fault, but I can’t see how I could do it differently… I’d be very grateful for any ideas! Many thanks.

There’s a minimal case which demonstrates the issue at

https://minimalcase.netlify.app/1

Git does not preserve lastmod time for files, so I’m unsure how you’re getting this:

So even when trying to do something like: tejal/astro.config.ts at v9 · hrishikesh-k/tejal, you still need to use mtimestore or something similar: tejal/mtimestore at v9 · hrishikesh-k/tejal. All of that is required unless you’re building locally.

Try that out and let me know if that changes anything.

Oh of COURSE. thank you. I can’t believe I didn’t think of this.

For my use case, as I have markdown documents, I could perhaps also use a git hook or GitHub action to insert the date into YAML front matter.

Once again, thank you so much for teaching me. Genuinely grateful.

Netlify won’t execute the post-checkout hook (in case you decide to use it). If you end up doing something like my example above, you’d have to run mtimestore as a part of the build command like:

./mtimestore && npm run build

Locally, I’m using a pre-commit hook to save the lastmod to the .mtimes file.

Ah man, thank you. I’d have remembered this… but only after I made the mistake! Many many thanks indeed. I can’t believe how helpful you’ve been!