Mattias Karlsson

@devlead

Bring static to life using serverless⚡

Breathe life into your static website using Azure Functions

Static 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.
Here in my opinion Azure Functions is a perfect fit, you can quickly create tailored and easily consumable just pay per use functions, in a variety of languages such as C#, F#, PowerShell, JavaScript and more.

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://medium.com/feed/@user.

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

Our Azure Function will download the RSS XML data, select only what’s needed (title, link and date) to display on website and return as easily from JavaScript consumable JSON format, a side effect is that this also results in only a fractions the amount of data downloaded, full control on which items and what data is exposed.

First line medium unfiltered RSS feed, second line Azure Function data

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

This function will basically call an RSS feed helper method that returns the mapped data as JSON and then the function returns it as an dynamic JavaScript file, exposing an feed variable.

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.

The helper method

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

As the function is exposed a JavaScript there’s no cross site issues, you’ll just use the script tag as you would do when fetching from i.e. a CDN.

The template

In this post I chose a really basic view template engine called mustache.js which is the JavaScript implementation of the mustache template language. It does support supplying object as model, iterating and applying an template to each item in an array.

Just bing an JavaScript object exposed as a variable makes it very easy to use any template engine you want whether it be React, Angular, Aurelia etc.
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

Conclusion

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

Previous posts on Azure Functions

More by Mattias Karlsson

Topics of interest

More Related Stories