Setting Up RSS With Hugo
The Hugo docs page on RSS is a little more complicated than it needs to be if all you want is an RSS feed. Hugo does this automatically and if you want to display that on the footer of your site or elsewhere all you have to do is provide a link. The link is simply https://yoursite.com/index.xml
. You can test it out by simply adding index.xml
to the end of your site’s url or localhost.
Additionally each section will have it’s own RSS. Again just add index.xml
to the end of each section to see it.
Additionally you want a link in the <head>
of each page linking to that sections RSS. See the docs for the options.
This RSS page looks great in Firefox which provides some nice styling but rather user unfriendly in Chrome based browsers (inc Edge) which provides no CSS at all.
1<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
2<channel>
3<title>home page on Blank Try</title>
4<link>http://localhost:64421/</link>
5<description>Recent content in home page on Blank Try</description>
6<generator>Hugo -- gohugo.io</generator>
7<language>en-us</language>
8<atom:link href="http://localhost:64421/index.xml" rel="self" type="application/rss+xml"/>
9<item>
10<title>Testpage</title>
11<link>http://localhost:64421/code/testpage/</link>
12<pubDate>Thu, 13 May 2021 16:42:53 +0100</pubDate>
13<guid>http://localhost:64421/code/testpage/</guid>
14<description>A page for experimenting with Hugo’s code</description>
15</item>
16etc...
If you want to create your own RSS template first know what you’re getting into.
This is the default, built in template used by Hugo. You can actually have a few options to change some things in the site config file.
1{{- $pctx := . -}}
2{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}}
3{{- $pages := slice -}}
4{{- if or $.IsHome $.IsSection -}}
5{{- $pages = $pctx.RegularPages -}}
6{{- else -}}
7{{- $pages = $pctx.Pages -}}
8{{- end -}}
9{{- $limit := .Site.Config.Services.RSS.Limit -}}
10{{- if ge $limit 1 -}}
11{{- $pages = $pages | first $limit -}}
12{{- end -}}
13{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
14<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
15 <channel>
16 <title>{{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title>
17 <link>{{ .Permalink }}</link>
18 <description>Recent content {{ if ne .Title .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }}</description>
19 <generator>Hugo -- gohugo.io</generator>{{ with .Site.LanguageCode }}
20 <language>{{.}}</language>{{end}}{{ with .Site.Author.email }}
21 <managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }}
22 <webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}{{ with .Site.Copyright }}
23 <copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }}
24 <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
25 {{- with .OutputFormats.Get "RSS" -}}
26 {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
27 {{- end -}}
28 {{ range $pages }}
29 <item>
30 <title>{{ .Title }}</title>
31 <link>{{ .Permalink }}</link>
32 <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
33 {{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
34 <guid>{{ .Permalink }}</guid>
35 <description>{{ .Summary | html }}</description>
36 </item>
37 {{ end }}
38 </channel>
39</rss>
To add CSS you’ll have to create a new template..
Registering
So from W3 Schools:
Submit your RSS feed to the RSS Feed Directories (you can Google or Yahoo for “RSS Feed Directories”). Note! The URL to your feed is not your home page, it is the URL to your feed, like “https://www.w3schools.com/xml/myfirstrss.xml". Here is a free RSS aggregation service:
Register your feed with the major search engines:
Google - http://www.google.com/submityourcontent/website-owner Bing - http://www.bing.com/toolbox/submit-site-url
Update your feed - After registering your RSS feed, you must make sure that you update your content frequently and that your RSS feed is constantly available.
Link in the <head>
This will help search engines find your RSS feed. It’s easy in Hugo. Just add this to your head for an RSS link on the home page and section pages:
1{{ with .OutputFormats.Get "rss" -}}
2 {{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }}
3{{ end -}}