Bring static to life using serverless
S tatic websites really are the latest fashion, and they’re really great and have many benefits. But sometimes you need to provide some dynamic content, store form data, etc. bringing small dynamic widgets/islands of life to otherwise static site.
In this post will as an example demonstrate how you on a website can add the last ten Medium blog posts, that also must have tags specified.
We’ll be using the data provided by the users Medium RSS feed, found at https:[email protected] .
In this post I’ll assume you’ve got some knowledge of Azure Functions, I’ve placed links to my previous Azure Functions post at the end of this post if you want to learn more on getting started with functions.Server(less) side
First line medium unfiltered RSS feed, second line Azure Functiondata
This also makes it perfect for mobile apps consuming third party APIs where you can save bytes by resulting in a more responsive app and happier users as you’re not eating up their cellular data-plan.The Function
The helper method is placed in an external file, which makes it easily reusable from other functions.
By design functions aren’t allowed to be cached by the browser, which makes perfect sense in most cases. But by setting the response object Headers CacheControl property, we can override the default behavior, letting the function result be cached for 10 minutes.
Which means while user navigates you site they’ll only hit that functions once every ten minutes, the rest of the time fetched from local cache and this will result in you saving money and the user gets quicker page loads.
As Functions give you the full power of.NET and C# you can with very few lines of code, fetch, parse and map the data how you want. Below I’m using LINQ to XML/objects to parse title, link and published date (formatting it as I want) for items having at least one category specified, then serializing the result to JSON using JSON.Net.Client side
Azure functions does support CORS and of course just returning JSON consumable as any rest API too.Result
The end result of the template with look something like below
I the cloud the Sky truly is the limit and Azure Functions truly are more versatile than first meets the eye.The Code
Complete code example for this function can be found on GitHub