Skip to content

Commit 20a6c91

Browse files
committed
Add site.feed.updated = latest_post setting
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.
1 parent 21a7fc9 commit 20a6c91

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ feed:
4545

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

48+
### Updated timestamp
49+
50+
By default, the feed will set `<updated>` to the current time at build time, as provided by Jekyll via `site.time`.
51+
52+
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).
53+
54+
```yml
55+
feed:
56+
updated: latest_post
57+
```
58+
4859
### Optional front matter
4960

5061
The plugin will use the following post metadata, automatically generated by Jekyll, which you can override via a post's YAML front matter:

lib/jekyll-feed/feed.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<generator uri="https://jekyllrb.com/" version="{{ jekyll.version }}">Jekyll</generator>
77
<link href="{{ page.url | absolute_url }}" rel="self" type="application/atom+xml" />
88
<link href="{{ '/' | absolute_url }}" rel="alternate" type="text/html" {% if site.lang %}hreflang="{{ site.lang }}" {% endif %}/>
9-
<updated>{{ site.time | date_to_xmlschema }}</updated>
109
<id>{{ page.url | absolute_url | xml_escape }}</id>
1110

1211
{% assign title = site.title | default: site.name %}
@@ -51,7 +50,20 @@
5150
{% assign posts = posts | where_exp: "post", "post.draft != true" %}
5251
{% endunless %}
5352
{% assign posts = posts | sort: "date" | reverse %}
53+
5454
{% assign posts_limit = site.feed.posts_limit | default: 10 %}
55+
56+
{% assign last_updated = nil %}
57+
{% if site.feed.updated == "latest_post" %}
58+
{% for post in posts limit: posts_limit %}
59+
{% assign post_updated = post.last_modified_at | default: post.date %}
60+
{% if last_updated == nil or post_updated > last_updated %}
61+
{% assign last_updated = post_updated %}
62+
{% endif %}
63+
{% endfor %}
64+
{% endif %}
65+
<updated>{{ last_updated | default: site.time | date_to_xmlschema }}</updated>
66+
5567
{% for post in posts limit: posts_limit %}
5668
<entry{% if post.lang %}{{" "}}xml:lang="{{ post.lang }}"{% endif %}>
5769
{% assign post_title = post.title | smartify | strip_html | normalize_whitespace | xml_escape %}

spec/jekyll-feed_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118

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

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

140+
context "with site.feed.updated = latest_post" do
141+
let(:overrides) do
142+
{
143+
"feed" => {
144+
"updated" => "latest_post",
145+
},
146+
}
147+
end
148+
149+
it "outputs the last post time" do
150+
expect(feed.updated.content).to eql(Time.parse("2016-04-25"))
151+
end
152+
end
153+
154+
it "outputs the current time" do
155+
expect(feed.updated.content).to eql(Time.parse("2018-12-31"))
156+
end
157+
139158
it "includes the items" do
140159
expect(feed.items.count).to eql(10)
141160
end

0 commit comments

Comments
 (0)