1

This is the solution I've used to sort posts by last-modified-date: Sorting Jekyll posts by modification date instead of posted date?

Jekyll::Hooks.register :posts, :pre_render do |post|

# get the current post last modified time
modification_time = File.mtime( post.path )

# inject modification_time in post's datas.
post.data['last-modified-date'] = modification_time

end

In development it works perfectly, as expected. All post dates are correct.

If I go through git before deploying to my host it's not working. All posts display last modification dates = commit dates (as far as I can tell). It essentially shows all posts were updated at the exact same time.

If I skip git and deploy the site on my host directly, again, it works as expected.

Note that I'm not using Github Pages to host the website.

Is there any way I could fix this issue? I'd like to keep version control for my project.

ashmaroli
  • 5,209
  • 2
  • 14
  • 25
Fraktar
  • 179
  • 12
  • Are you pushing contents of locally generated `_site/` to your remote host or is the site being "generated by Jekyll at the remote host" with each `git push`? – ashmaroli Jan 20 '18 at 15:58
  • @ashmaroli a) I do `git push origin master` to push content to github repo and then I have a hook that automatically deploys the website online (on Netlify) on each git push. b) I deploy the `_site` folder directly on Netlify. a) doesn't work. b) works. Does that make sense? – Fraktar Jan 20 '18 at 16:02
  • It does.. though I feel the hook is modifying each post as part of the deploy process instead of the one you just pushed.. – ashmaroli Jan 20 '18 at 16:06
  • If that's the case, what options do I have here? Would it help if I shared the repo so you can take a look? – Fraktar Jan 20 '18 at 16:09
  • Netlify only runs `gulp` when I update the repo which includes `jekyll build` and a few optimizations so it shouldn't affect the post's dates. Why doesn't this happen when I run the build command locally? – Fraktar Jan 20 '18 at 16:16
  • If possible, go through your "deployment" log entries.. I'm guessing that like how Travis CI creates a fresh clone of your repo with the given commit SHA, Netlify does similar to fetch your branch and build your site.. In theory, for the given Netlify session, only contents of that commit exists.. – ashmaroli Jan 20 '18 at 16:44
  • @ashmaroli here's the log for one of the deploys: https://pastebin.com/6geahyjE - can't find anything relevant. – Fraktar Jan 20 '18 at 16:54

2 Answers2

4

Disclaimer: I work for netlify.

The real problem here is git. It doesn't store timestamps for files, so if you used 'git clone' on your local development machine to check out your files TO AN EMPTY DIRECTORY (rather than in your repository that already exists), and built from there, you'd get the same results - all files created "right now" and post ordering using your current config would not be the same order you created them in.

How to work around this? Your config uses mtime on the filesystem to order posts which works great as long as you never go back and change an old post ("oops, typo in my first post, need to edit...oh, now it's my latest post?") or use git to store your files with the expectation that a checkout would preserve that ordering. So - that seems like a bad way to sort for your purposes for at least 2 reasons, so I'd pursue some other sorting option.

It looks from the Jekyll docs as though they do some ordering based on post tiles if you name them in their recommended format but who wants a post named title-31-03-2018.md? It seems like the RIGHT way to sort posts is to include an otherwise-optional date in the frontmatter according to this doc:

https://jekyllrb.com/docs/frontmatter/#predefined-variables-for-posts

...which will allow you to sort by post.date - which you've now specified and will withstand any number of edits and transfers in its original format - rather than the fickle mtime. This will work great with Git, though you do have to go put a date in all your frontmatter if it isn't already there. Sounds like this effort is justified, since you're very excited about reliable date sorting. This is futureproof and gitproof and will work on any hosting service as well as your new laptop that you have to restore from git once the hard drive fails.

fool
  • 3,044
  • 20
  • 28
  • Thanks for your input. That's not exactly what I was looking and was already aware of using a custom frontmatter variable to sort posts by that. Having said that, it's either this or stop using Git & Staticman. I'm going to use and "updated" variable in each post and sort by that for a while. – Fraktar Jan 24 '18 at 22:03
0

Netlify builds Git-based repos using a Docker Container that essentially contains the repo state as indexed in the given commit

So, since the repo is cloned in each session, all files are recently created/modified..

ashmaroli
  • 5,209
  • 2
  • 14
  • 25
  • So I have to choose between using Netlify & sorting my posts by last modification date, is there no way of making both work? – Fraktar Jan 20 '18 at 17:04
  • The solution I see is to have a Rake task set up to "build the site locally" but push the source to your GitHub Repo and push the contents of `_site` to Netlify.. (yes.., its a tacky hack) – ashmaroli Jan 20 '18 at 17:09
  • Yes, but that would imply I also have to drop services like Staticman https://staticman.net/docs/ right? (using that for comments as alternative to disqus) – Fraktar Jan 20 '18 at 17:21
  • @fool For knowledge purposes.. does your comment mean that if a user were to set a `clone_depth=5`, then the `mtimes` for individual files are preserved across commits..? – ashmaroli Jan 24 '18 at 16:33
  • @ashmaroli As far as I can tell since you can't set any git options when Netlify clones your site - kind of a moot point. But AFAICT there is no option to vanilla git that preserves dates. – fool Jan 24 '18 at 16:39