1
votes

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.

2
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
@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
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
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
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

2 Answers

4
votes

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.

0
votes

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..