How to make a RSS feed for your Neos site

Let's say you have gotten your basic nodeType based blog up and running in Neos. And now someone suggests you create a RSS feed for it. Sounds easy enough and turns out to be pretty straightforward. I only had two minor issues.

Add a new page layout

Start out by creating an extra layout type in your sites' NodeTypes.yaml. There you configure the layouts you want to use in the TYPO3.Neos.NodeTypes:Page object:

Add rss layout to NodeTypes.yaml YAML

Add the rss page using the rss layout

You can now create a new page that uses the layout you have just created.

  • create a page called 'rss' in the root of your site;
  • set the page to be 'hidden from menus' (metadata tab)
  • set the page layout to 'Rss'

Define the rss Page layout

Open your Root.ts2 where your page layouts are all configured, and add a layout called 'rss'. I used Packages/Application/TYPO3.Neos/Resources/Private/TypoScript/Prototypes/Page.ts2 as a template. We set the layout to be a TYPO3.TypoScript:Http.Message.

We override the Content-Type header with text/xml:

TypoScript2

Then you can add the docheader and the rest of the tags. You are free to name the other objects as you wish. The only fixed object is the 'httpResponseHead'. As said, we are basically working with a TYPO3.TypoScript.Array which allows for setting an order to the elements using the @position property.

If you omit the @position tags, the elements will be rendered in the order of appearance. So I guess they had some use in the Page.ts2 where I copied them from, but for your RSS feed we could leave them out. It is however good practice to add them if you expect your layout to be extended by other layouts. This will make it easier for the extenders to position their elements in between yours.

Let's add some more tags.

TypoScript2

The doctype is a basic string object. The rssTag is a TYPO3.TypoScript:Tag object. This allows for setting a content string value (which in turn may be rendered by a TYPO3.TypoScript:Tag) and setting attributes. For improved readability we omit the closing tag and manually close the rss tag with a string value later on.

Then we need a to fill the channel with items. We get them from a TYPO3.TypoScript:Template object. This object has a template and some data properties that will be available to us in Fluid. The FlowQuery fetches all documents of type TypoFree.MichielRoos:Article from the Article folder. For now I use a hardcoded path.

Content of the channel tag TypoScript2

I could add a special nodeType to place on the rss page in which I could select a storage node from which to retrieve the articles, but alas . . . clicking the rss page in the Neos backend results in an error message.

The cause of this is that Neos 'injects' tags into the <head> of the document it is editing. But we don't have such a tag in our RSS layout. Find the relevant TypoScript in  Packages/Application/TYPO3.Neos/Resources/Private/TypoScript/Prototypes/Page.ts2.

RSS Feed document metadata error

So the solution could be made nicer by making the storage node configurable, but a hardcoded value will have to do for now. Dmitri Pisarev notes you should be able to get around this problem using conditions in Fluid <f:if condition="{node.context.workspace.name} != 'live'"> or on TypoScript properties: [email protected] = ${node.context.workspace.name == 'live'}

To top it off we have some closing tags and a cache configuration. The cache configuration ensures that the rss page cache will be flushed whenever a node of type 'TypoFree.MichielRoos:Article' changes.

Cache configuration TypoScript2

Our rss layout is now done. It's cache will be flushed whenever a node of the Article type is changed.

You can find the full rss page layout down below.

Create a Fluid template

The Fluid template is pretty straight forward (excuse the broken highlighting, still need to do the Fluid, FlowQuery, Eel and TypoScript 2 modes for codemirror ;-):

RSS Fluid Template HTML

Adding the rss link to the document head

We now have a rss pagetype that adds a nice valid RSS feed. For the rss feed to be accessible for browsers, we can add a rss link to the page head. The browsers of 2014 seem to lack the rss url buttons they used to have some years ago, so make sure you also put a physical link to your feed on your page.

To add a rss link to the page head, we need to adjust the default page layout:

Add a rss link to the head element TypoScript2

Validate your feed

Now all you need to do is validate your feed over at the W3C. Looks pretty good to me.

Fixing the rss file extension

One thing that annoys me is that the name of the feed ends in .html.

Sebastian Helzle and Dmitri Pisarev pointed me to some working code snippets.

Routes.yaml YAML

This route works to the point that the rss can now be retrieved with .html or with .xml. But the link in the footer of this site, generated by Fluid, will still be rendered with the .html suffix.

The missing piece of the puzzle here for me was to add format="xml" to the <neos:link.node/> that generates the link.

Add format="xml" to force the ".xml" file extension HTML

Resources

The full RSS page layout

The rss page layout TypoScript2

Want to react? Reach me on Twitter