Skip to content

Commit

Permalink
Add site.feed.updated == latest_post setting
Browse files Browse the repository at this point in the history
Primary uses cases:

1. Reduce deployment churn in repositories that hold a docsite in
   addition to source code. These repositories currently regenerate
   and find "changed" files to deploy on every commit, even when
   nothing in the Jekyll site was changed. The only artefact changing
   each time is `feed.xml`.

   For example: https://github.com/qunitjs/qunit/commits/gh-pages

2. Improve reproducibility of the build, as highlighted via
   jekyll/jekyll#7187. By offering a choice
   that simply eliminates use of current time entirely, we do not
   even have to worry about mocking it.
  • Loading branch information
Krinkle committed Sep 19, 2022
1 parent 21a7fc9 commit 4bd3c04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ feed:

To note, you shouldn't have to do this unless you already have a feed you're using, and you can't or wish not to redirect existing subscribers.

### Updated timestamp

By default, the feed will set `<updated>` to the current time at build time, as provided by Jekyll via `site.time`.

Enable the `updated: latest_post` setting to use a deterministic value based on when the latest post was published or updated. (The [jekyll-last-modified-at](https://github.com/gjtorikian/jekyll-last-modified-at) plugin is supported to help provide post modification times).

```yml
feed:
updated: latest_post
```

### Optional front matter

The plugin will use the following post metadata, automatically generated by Jekyll, which you can override via a post's YAML front matter:
Expand Down
14 changes: 13 additions & 1 deletion lib/jekyll-feed/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<generator uri="https://jekyllrb.com/" version="{{ jekyll.version }}">Jekyll</generator>
<link href="{{ page.url | absolute_url }}" rel="self" type="application/atom+xml" />
<link href="{{ '/' | absolute_url }}" rel="alternate" type="text/html" {% if site.lang %}hreflang="{{ site.lang }}" {% endif %}/>
<updated>{{ site.time | date_to_xmlschema }}</updated>
<id>{{ page.url | absolute_url | xml_escape }}</id>

{% assign title = site.title | default: site.name %}
Expand Down Expand Up @@ -51,7 +50,20 @@
{% assign posts = posts | where_exp: "post", "post.draft != true" %}
{% endunless %}
{% assign posts = posts | sort: "date" | reverse %}

{% assign posts_limit = site.feed.posts_limit | default: 10 %}

{% assign last_updated = nil %}
{% if site.feed.updated == "latest_post" %}
{% for post in posts limit: posts_limit %}
{% assign post_updated = post.last_modified_at | default: post.date %}
{% if last_updated == nil or post_updated > last_updated %}
{% assign last_updated = post_updated %}
{% endif %}
{% endfor %}
{% endif %}
<updated>{{ last_updated | default: site.time | date_to_xmlschema }}</updated>

{% for post in posts limit: posts_limit %}
<entry{% if post.lang %}{{" "}}xml:lang="{{ post.lang }}"{% endif %}>
{% assign post_title = post.title | smartify | strip_html | normalize_whitespace | xml_escape %}
Expand Down
19 changes: 19 additions & 0 deletions spec/jekyll-feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@

context "parsing" do
let(:feed) { RSS::Parser.parse(contents) }
let(:overrides) { { "time" => Time.parse("2018-12-31") } }

it "outputs an RSS feed" do
expect(feed.feed_type).to eql("atom")
Expand All @@ -136,6 +137,24 @@
expect(feed.generator.version).to eql(Jekyll::VERSION)
end

context "with site.feed.updated = latest_post" do
let(:overrides) do
{
"feed" => {
"updated" => "latest_post",
},
}
end

it "outputs the last post time" do
expect(feed.updated.content).to eql(Time.parse("2016-04-25"))
end
end

it "outputs the current time" do
expect(feed.updated.content).to eql(Time.parse("2018-12-31"))
end

it "includes the items" do
expect(feed.items.count).to eql(10)
end
Expand Down

0 comments on commit 4bd3c04

Please sign in to comment.