Though the engineering aspect has changed over time with many frameworks and tools, the underlying fact of, they are static files haven’t changed. This gives the opinion of serving those static content, from the locations closer to the consumer, rather than from a remote web server.
This will give high performance by reducing network latency. In this article let’s see how to deliver a SPA or any static content with Azure DevOps with the best optimum setup in terms of performance and cost.
Follow the below approach……
- Enabling and hosting the static website in Blob Storage
- Setting up Azure DevOps pipeline
- Setting Custom Domain
- Optimization with edge using CDN and enable SSL
- Azure DevOps considerations in CDN delivery
Enabling & hosting static website in Blob
You can create a standard blob storage in Azure. You will get the static website feature by default, you should enable it for the use.
Static website hosting has a special container named $web, which is the www root of the static website.
Normally, Blob storage does not allow us to create containers with non alphanumeric characters, but this is a special container created for static website hosting.
You will get two endpoints primary and secondary. Both will point to the index document. You can upload the index document. You can optionally configure the error document as well.
In this case index.html is used for both. For testing purposes, just upload a simple html file with the name index.html, then browse any of those endpoints, then you would see the uploaded index.html.
This confirms the Blob storage static web hosting has been enabled and working properly.
Setting Azure DevOps Pipeline
Now, we have to setup the DevOps pipeline for Continuous Integration and
Continuous Deployment. Regardless of the framework you use for development (React, Angular, Vue, WebAssembly or anything that came today morning) – end of the day the build artifacts should be bundled as static files.
Different frameworks require different build steps and it varies based on the project context as well. Once the build is completed the artifacts should be uploaded $web container of the Blob storage.
In Azure DevOps you can use Azure Blob File Copy build step to achieve this. This will copy the pointed artifacts to the specified container.
Note, use the version 2* (still in preview as of this writing) the previous versions would complain that a container name cannot be validated with $ character
Custom Domain in Azure Blob static website hosting
Let’s setup a custom domain to our static web site, this would be one important step you require to accomplish in production.
You can use your DNS management or migrate your domain to Azure DNS Zone.
I have used Azure DNS Zone – Go to your DNS settings and create a CNAME record with one of the endpoints as below.
You cannot create a DNS ‘A’ record here, because Storage doesn’t provide a IP.
Optimization with edge using CDN and enable SSL
You can further optimize the delivery by bringing the content files to the CDN. Configure a CDN endpoint to the Azure Blob storage.
Delivering via CDN allows to have SSL enabled as well.
Create a CDN endpoint in a CDN profile.
Select the Custom Origin and enter the static website host name of the Blob storage. DO NOT select the Storage as origin type.
Now if you browse through the CDN endpoint (https://aventude-spa.azureedge.net) you will see the web page (the change need sometime)
Since we have changed the delivery address to the CDN, now we have to map the domain to the CDN endpoint. This is quite straightforward as the previous step. You have to create a CNAME entry pointing the CDN endpoint.
Once done, you can navigate to the custom domains in the CDN endpoint and enable the custom domain HTTPS.
Azure DevOps Considerations in delivering in CDN Delivery
When the delivery is optimized via CDN – Whenever we do the artifact publishing to the Blob storage – either we have to purge the CDN or wait for the content to propagate.
Most of the cases, purging is the recommended approach. Azure DevOps has a handy Purge Azure CDN endpoint build step.
This step will trigger the purge operation and changes will be immediately available once purge is completed.
Azure CDN provides DSA (Dynamic Site Acceleration) delivery, in this case purge is not required. Because the content is not stored in the CDN.
In the DSA mode, what CDN does is, optimize the route path from the caller to the origin in the best possible way. If your static content has frequent changes then this approach is recommended over purging.
DSA should be enabled at the time of CDN endpoint creation.
Following drawing summarizes the whole idea here.
You can choose the delivery at the Blob storage level or at the CDN level. The mechanism from the Blob storage to CDN changes based on the update frequency of the content.
Also, note – as stated earlier both the Blob storage and CDN does not allow us to have A record mapping in the DNS. This may be a drawback, but in most practical cases, the front end applications are delivered with the sub domain URL like app.<domain>.com.
In case you need to resolve A record like https://28368833.com, you should do a URL rewrite from the mapped A record destination IP.