Navigate back to the homepage

An Introduction to Azure Static WebApps

John Kilmister
February 9th, 2021 · 4 min read

Azure Static Web Apps add first class support for modern, fast and responsive websites inside the Azure portal. The integration allows you to create and deploy a web site very fast with little configuration.

What is a Static Web App

Traditional web applications, such as those based on ASP.net generate pages on the web server with each request. These pages may be cached however the initial fetch of the page can be slow. Static sites are an alternative approach and fall into two categories.

Single page applications (SPAs) are written using a JavaScript framework, such as Angular and run in the browser. The SPA makes further API calls to populate the page with content.

JAM (JavaScript, APIs, and Markup) stack applications take a different approach and use static site generation to create static HTML/JS pages at build time. This means they are very light, quick to load and are more secure.

There are many diffrent site generators however I have been using Gatsby, a React based generator for just over a year and it has a large community with many plugins and themes.

What are Azure Static Web Apps

A feature of Azure I have utilised heavily over the years has been Azure Static Website not to be confused with the new Azure Static Web Apps. Azure has had static website hosting for some time with storage accounts. You enable the feature under the blob storage account, place you files in the $web folder and you have a website.

While Azure Static Websites are a great feature you need to attach to either an Azure front door or an Azure CDN if you want to use SSL, set http headers, utilize caching, or have any server side redirects. There also are some features which it just cannot support.

Azure Static Web Apps (currently in preview) solves these issues and brings everything together in addition to setting up a full build and deploy pipeline for you.

Creating a Static Web App

Before you can deploy a static web app you need a GitHub repository with a site code ready to publish. If you have this already you can skip head otherwise read on.

1. Create the Application

There are many static site generators (Over 300 on jamstack.org) however it is worth noting that currently Azure Web Apps have build presets for only three static site generators; Gatsby, Hugo and VuePress. If you are using another you will need to provide your own build script.

For this tutorial we will use Gatsby, one of the most popular react based generators.

You will first to setup your local machine with Gatsby and once setup you can run one of the many starter sites. For this blog for example I have used the Novela starter which can be created using this command:

1gatsby new novela-site https://github.com/narative/gatsby-starter-novela

As with all Gatsby sites you can then run this locally using the develop command:

1gatsby develop

2. Create a Github Repository

At the moment Azure Static Web Apps require your code and build script to stored in GitHub however they have said that they will be expanding this to DevOps soon.

Create a new Github repository and push up your gatsby site. Azure Static Web Apps support both private and public repositories so you can select either. You can skip the readme and gitignore options as these are part of the starter application you already have.

3. Create a Static Web App in Azure

Creating the app in Azure is the simplest part of the process. In the Azure portal navigate to “Create Resource” and select “Static Web Apps”, or jump to the create page.

Screenshot of create page

When prompted enter the name and select both the region and resource group. As this feature is in public preview not all regions are available yet.

The next step is to connect your GitHub account and select both the repository and branch.

As part of this process it will set up GitHub Actions to automatically build and publish your web app when code is committed. For this to work you will need to define a few details. As we are using Gatsby static site generator we will select this and the app location, api location and output are auto populated for us.

Once “Create” is clicked the site will be generated and you will then be able to browse to it.

4. Domain Names

To attach a domain name from the portal when looking at your static web app select “Custom Domains” then click “Add”. You are then given a CName entry to add to you DNS record.

A SSL certificate is manged for you and HSTS is turned on so you don’t need to worry about these.

By default as it uses CNames, apex domains are not supported. You can work around this by using this Azure DNS Guide or using this CloudFlare guide.

5. 404 Pages and Redirects

301, 302 and 308 redirects can only be issued from a server and not from JavaScript however Azure Static Web Apps have a solution to this. Static Web Apps support a routes.json file. This defines redirects based on URL and status code as well as restricting access to routes via role based permissions.

Azure looks for this file in a variety of locations dependant on the framework you are using. Gatsby is based on react so we can add it to the static folder which will copy it into the public folder as part of the build.

Screenshot of default 404 page

By default even though our Gatsby app can handle 404s gracefully they are intercepted by the Static Web App and a default page is shown. Placing an entry in our routes.json file fixes the issue.

1{
2 "routes": [],
3 "platformErrorOverrides": [
4 {
5 "errorType": "NotFound",
6 "serve": "/404"
7 },
8 {
9 "errorType": "Unauthenticated",
10 "statusCode": "302",
11 "serve": "/login"
12 }
13 ]
14 }

There is a lot that can be done with the routes file including adding default http headers and setting the mime type. The routes however are relative and cannot be based on hostname and only default headers for all requests can be added.

Other Features

Although I am not covering these in detail it is worth mentioning that you can host Azure functions as part of the same static web app. As they are integrated this means they require no CORS configuration.

Authentication is also now first class feature allowing you to secure routes using Azure AD, GitHub, google, Facebook or Twitter. More information can be found in the official documentation

Summary

In this post we have looked at what static web apps are and how using the preview feature in Azure we can take a Gatsby starter template and deploy this with a custom domain.

Title Photo by glenncarstenspeters on Unsplash.

If you enjoyed reading please share...

More articles from John kilmister

OAuth Device Flow for IoT with the Microsoft Graph API

Device code flow is a flow for browserless and input constrained devices that can be used by your applications on IoT devices

January 28th, 2021 · 3 min read

Project Teams to Product Teams with Team Topologies

As our department grew both in numbers of engineers and in products owned there was a need to revisit this structure, to reduce cognitive load and increase ownership in order to increase throughput we designed a structure around the ideas set out in team topologies book and documented the journey.

July 10th, 2020 · 1 min read
Link to $https://twitter.com/johnkilmisterLink to $https://github.com/blueboxesLink to $https://stackoverflow.com/users/33/johnLink to $https://www.linkedin.com/in/johnkilmister/