MediaTypes: how to choose which suffix

For one project, I need an xml feed to live at /feed.rss instead of /index.xml. Now I tried adding the rss suffix to the application/rss+xml mediaType before using it on my custom output format.

mediaTypes:
  application/rss+xml:
    suffixes:
    - xml
    - rss

outputFormats:
  rss_feed:
    mediaType: application/rss+xml
    baseName: feed

Now how can I have the file generated at /feed.rss instead of /feed.xml. I tried using a layout called index.rss_feed.rss but it is not being picked up (the output format file is not created)

Any ideas?

Thanks a ton!

@regis

The following emits both feed.rss and index.xml for me:

config.toml

[mediaTypes."application/rss+xml"]
suffixes = ["rss","xml"]

[outputs]
home = ["HTML", "RSS", "rss_feed"]

[outputFormats]

[outputFormats.rss_feed]
mediaType = "application/rss+xml"
baseName = "feed"
notAlternatives = true

copied from internal Hugo template

layouts/index.rss_feed.rss

{{- $pctx := . -}}
{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}}
{{- $pages := slice -}}
{{- if or $.IsHome $.IsSection -}}
{{- $pages = $pctx.RegularPages -}}
{{- else -}}
{{- $pages = $pctx.Pages -}}
{{- end -}}
{{- $limit := .Site.Config.Services.RSS.Limit -}}
{{- if ge $limit 1 -}}
{{- $pages = $pages | first $limit -}}
{{- end -}}
{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>{{ if eq  .Title  .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title>
    <link>{{ .Permalink }}</link>
    <description>Recent content {{ if ne  .Title  .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }}</description>
    <generator>Hugo -- gohugo.io</generator>{{ with .Site.LanguageCode }}
    <language>{{.}}</language>{{end}}{{ with .Site.Author.email }}
    <managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }}
    <webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}{{ with .Site.Copyright }}
    <copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }}
    <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
    {{- with .OutputFormats.Get "RSS" -}}
    {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
    {{- end -}}
    {{ range $pages }}
    <item>
      <title>{{ .Title }}</title>
      <link>{{ .Permalink }}</link>
      <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
      {{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
      <guid>{{ .Permalink }}</guid>
      <description>{{ .Summary | html }}</description>
    </item>
    {{ end }}
  </channel>
</rss>

HTH


Previous attempts

Do you need both types of RSS files (index.xml and feed.rss)? I think what is happening is the default xml layout is getting picked up first.

EDIT: What happens if you reverse the order of suffixes?

EDIT: Sorry realized you looked at this already after the fact.


Looking at Custom output formats | Hugo

The key distinction for Hugo versions 0.20 and newer is that Hugo looks at an output format’s Name and MediaType’s Suffixes when choosing the template used to render a given Page .

I think you will have to create a new template name something like:

layouts/_default/rss_feed.rss

1 Like

I think that the only current way to create outputs for different suffix for a given media type is to create multiple media types. We could (and probably should) add a “suffix selector” to output format config …

1 Like

Thank you all for your answers. I created an issue to follow up this discussion:

I’m trying to do something similar with text/html, producing some files with the extension html and others with htm (backwards compatibility).

For example, I’d like to create files as such:
/index.html
/index.htm (would become a redirect to /index.html)
/states/washington_state.htm
/states/wa/index.html

Wouldn’t it be easier to set up a rewrite rule on your web server that replaces htm with html?

It would certainly be faster, but less portable if I want to use the Hugo server and switch between Netlify and AWS S3.