Skip to content

Instantly share code, notes, and snippets.

@emmahsax
Last active November 11, 2020 19:01
Show Gist options
  • Save emmahsax/7c3b70a8f983fea9610209e9d7618cf4 to your computer and use it in GitHub Desktop.
Save emmahsax/7c3b70a8f983fea9610209e9d7618cf4 to your computer and use it in GitHub Desktop.
Jekyll Feed with multiple environments

Jekyll Feed with Multiple Environments

What this will do is generate a two different feeds:

  • A feed.xml for your local development, which will contain the values you specify for your development environment in your _config.yml.
  • A feed.xml for your live site, which will have the values set in your _config.yml for your production environment.

This is necessary because without different values for title, description, etc, many feed readers will view the two feeds as the same, even if the post content is different This makes it difficult to test if your feed is working and updating locally.

You could add additional environments as well, such as development, test, staging, and production.

Here's what your feed.xml should look like:

---
layout: none
---

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  {% assign feed = site.feed[jekyll.environment] %}
  <channel>
    <title>{{ feed.title | xml_escape }}</title>
    <description>{{ feed.description | xml_escape }}</description>
    <link>{{ feed.url }}</link>
    <atom:link href="{{ feed.url }}/feed.xml" rel="self" type="application/rss+xml" />
    {% for post in site.posts limit: feed.items %}
      <item>
        <title>{{ post.title | xml_escape }}</title>
        <description>{{ post.description | xml_escape }}</description>
        <pubDate>{{ post.date | date: "%a, %d %b %Y %H:%M:%S %z" }}</pubDate>
        <link>{{ feed.url }}{{ post.url }}</link>
        <guid isPermaLink="true">{{ feed.url }}{{ post.url }}</guid>
      </item>
    {% endfor %}
  </channel>
</rss>

And here's what your _config.yml file should look like:

title: An Awesome Blog
description: My blog, which is awesome
url: https://an-awesome-blog.com

feed:
  production:
    title: An Awesome Blog
    description: My blog, which is awesome
    url: https://an-awesome-blog.com
    items: 10
  development:
    title: LOCAL An Awesome Blog
    description: LOCAL blog, which is still awesome
    url: http://127.0.0.1:4000
    items: 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment