Building offline Blazor application

I made one small experiment and tried to get simple client-side Blazor application running on browser in offline mode. Yes, offline – visit application page once, turn out internet connection, open browser, type in application URL and it just runs. This blog post is short illustrated overview of my experiment.

For this experiment I built simple beer calculator. Based on original and final special gravity (OG and FG respectively) it calculates the alcohol content of beer. The calculator doesn’t use any equations that can be taken as business secret and therefore we don’t need server-side Blazor application.

Source code available! I made Blazor offline beer calculator available with full source on GitHub. Just head to my repository gpeipman/BlazorBeerCalculator. You can try it out on your machine here:

After finishing my work I did something crazy. Here’s the screenshot of application made on my Lumia 950. Just tried if Blazor applications can also run on mobile version of Edge and yes – it works.

Offline Blazor applicationOffline Blazor application

Actually Blazor applications target all browsers out there and they run better on current machines. My Lumia almost died when it ran my offline Blazor application.

Getting Blazor offline

As i built beer calculator as a proof of concept application I went with HTML5 features to make application work offline.The trick here is to build application cache manifest the way that browsers get all files needed to run application offline. Make one small mistake and you run into troubles.

HTML5 application cache manifest is defined in HTML markup. Here is index.html file of my offline Blazor application.

<!DOCTYPE html>
<html manifest="application.appcache">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width">
    <title>Blazor Beer Calculator</title>
    <base href="/" />
    <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
    <link href="css/site.css" rel="stylesheet" />

    <script src="_framework/blazor.webassembly.js"></script>

Location of application cache is defined on manifest-attribute of html tag. It is the location of file that defines application cache contents.

Here is my application cache manifest.

/ /index.html

It tells browser to cache all files of my offline Blazor application. Fallback section defines fallback URL-s. If there is no page available in cache for given address then the one from fallback section is used. Index.html is default file in server. We have to specify this file as browser doesn’t know what is the default file. Also we need one fallback so browser knows that index.html must be used when browser is going to make request to our application folder without specifying a file. You can find out more about application cache from HTML5 Rocks article A Beginner’s Guide to Using the Application Cache.

Application cache will be deprecated. It didn’t made its way to HTML5 standards and Web Hypertext Application Technology Working Group’s (WHATWG) is in the process of removing application cache from Web platform. They also suggest that using application cache is highly discouraged. Read more about it from The Page Not Found Blog post Application Cache is not gone, oh my! Or, is it?

Wrapping up

Getting my offline Blazor application run without internet connection in browser was a little bit challenging. Application itself is easy and it took just few minutes to get it done. I spent more time on getting application cache right. I also had to define correct MIME-type for *.wasm files as Blazor runs into troubles when content type is not correct. Once I got application cache defined correctly my offline Blazor application started working without problems on all browsers I tried (Edge, Firefox, Chrome, Edge on Lumia). It is proof that Blazor applications can run offline already today.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    Leave a Reply

    Your email address will not be published. Required fields are marked *