🦖 Deno Fresh RSS Feed: Acing HTTP Security Headers #
Now is a great time to set up your Deno Fresh RSS Feed. That is because there is uncertainty over the direction of travel of a popular social network. This may leave your followers turning to alternative feeds to get their daily fix of updates. With RSS you publish a feed on your site, your followers subscribe. Now they have an auto-updating list of your latest content, all in chronological order. What’s more, Deno Fresh makes it easy for you to create that self-updating feed. In the video, you see how to serve an RSS feed from a resource route on your Deno site. On top, we look at caching headers and adding the right content to the head section on your site HTML pages to advertise the feed.
Anyway, that is enough of an introduction, if you still think you’re in the right place then hit play below on the video! You can drop a comment below or reach out for a chat on Element as well as Twitter @mention if you have suggestions for improvements or questions.
📹 Video #
🗳 Poll #
🖥 Deno Fresh RSS Feed: Code #
Blog Utility Functions #
utility/blog.ts
— click to expand code.
Net Utility Function #
utility/net.ts
— click to expand code.
RSS Feed (resource route) #
routes/xml.rss
— click to expand code.
HTML Head Snippet (advertising feed) #
🔗 Links #
- getting started with Deno Fresh post
- complete code for this project on GitHub
- Deno docs on front matter extract
- Raven RSS Reader
- Feeder RSS Reader
- Element chat: #rodney matrix chat
- Twitter handle: @askRodney
🏁 Deno Fresh RSS Feed: Summary #
Does Deno Fresh have an RSS Feed plugin? #
- At the time of writing there is no plugin, but we have seen it is pretty easy to set up something flexible yourself, using a resource route in Deno Fresh. Deno gives you back control using the platform to let you add features like an RSS feed exactly the way you want it! The resource route will have a handler function which listens for GET requests. This is not too different to handlers you might already be using on your HTML routes. You can build up the XML body of the response within the handler, then return it (as a new Response object). We saw how you just have to remember to add a couple of headers for content type and caching.
What are resource routes, and how do you add them in Deno Fresh? #
- While pages on your Deno Fresh site serve HTML content, you can use resource routes to serve a wider range of content formats. We saw an XML RSS Feed example, but you could also serve a PDF brochure, an e-book, or even some data in CSV or JSON format. To create the resource route, you follow the same file-base routing system used for your HTML pages. Just the file extension will be `.ts`. Let’s say you want to serve your autobiography as an e-book from `https://example.com/books/my-life.epub`. Following the file-based routing system, you would place the handler in `routes/books/my-life.epub.ts`. That’s it! The handler returns an HTTP response, and the body (in this case) would be the binary bytes of your book file. Remember also to use an appropriate content-type header!
How do you parse Markdown front matter in Deno? #
- Deno has an extract function defined in `std/encoding/front_matter/yaml.ts`. You can import this at the top of your source file and use it to parse valid YAML front matter into a TypeScript object. First read the file into a variable using `const text = await Deno.readTextFile('..path/to/markdown.md);`. Next, you can call the extract function on this text, destructuring `attrs` like this: `const {attrs} = extract(text);`. Note that `extract` supports generics, so if you have a type alias for your front matter, you can use it thus: `extract<MyFrontMatterType>(text)`. Finally, let’s say you have a title field in your front matter. You can access it using `attrs.title`.
🙏🏽 Deno Fresh RSS Feed: Feedback #
Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.
With 📜 RSS making a comeback I put together a short video on adding an RSS Feed to your Deno 🍋 Fresh site.
— Rodney (@askRodney) January 27, 2023
Should be handy if you want to create an XML sitemap for SEO purposes too!
Hope you find it useful!
#learndeno #askrodney #usetheplatformhttps://t.co/c1ny7Mf6gj
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter, @rodney@toot.community on Mastodon and also the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as Deno. Also, subscribe to the newsletter to keep up-to-date with our latest projects.