Offline Web Applications
Firstly, the path to the .manifest file needs to be included in the HTML element at the top of the page.
<html lang="en" manifest="offline.manifest">
For an MVC site, simply add the reference to the manifest in the _Layout file and add each resource you want in the browser application cache.
These resources above are used for the standard MVC template you get with Visual Studio 2015.
Now to get IIS to serve up this offline.manifest file, you either have to add a mime type on the server or add it to the
<system.webserver> element in web.config.
<mimeMap fileExtension=".css" mimeType="text/css"/>
<mimeMap fileExtension=".manifest" mimeType="text/cache-manifest" />
<mimeMap fileExtension=".html" mimeType="text/html" />
<mimeMap fileExtension=".jpg" mimeType="image/jpeg" />
<remove name="FormsAuthentication" />
<remove name="ApplicationInsightsWebTracking" />
Microsoft.AI.Web" preCondition="managedHandler" />
<validation validateIntegratedModeConfiguration="false" />
It's important that you add the
clear element and you need then to add the usual types your site will serve up, otherwise you will get an HTTP 500 on each of these.
In Chrome, when browsing to this page, you can examine the Application Cache by going to the developer tools (F12) and going to the Resources tab.
Make sure that the ‘Disable cache’ check box is cleared as by default it is checked when you open up the developer tools and the browser will not retrieve the files needed from the cache.
The main issue with the .manifest file is the browser does not know when the contents listed in it have changed unless the time stamp on the manifest itself has changed. One way to do this is to version the file and an even better way is to version it by using your application build number.
There are many issues with MVC serving up a .manifest file and the easiest solution I have found is to write out a physical file to the location the site is hosted in like this:
string path = ControllerContext.HttpContext.Server.MapPath("~/Manifest/offline.manifest");
var version = typeof(MvcApplication).Assembly.GetName().Version;
var manifest = "CACHE MANIFEST" + Environment.NewLine +
"# Server Assembly Version: " + version.Major + "." +
version.Minor + "." + version.Build + Environment.NewLine +
"CACHE:" + Environment.NewLine +
Url.Action("Index", "Home") + Environment.NewLine +
Url.Content("/Content/bootstrap.css") + Environment.NewLine +
Url.Content("/Content/site.css") + Environment.NewLine +
Url.Content("/Scripts/modernizr-2.6.2.js") + Environment.NewLine +
Url.Content("/Scripts/jquery-1.10.2.js") + Environment.NewLine +
Url.Content("/Scripts/bootstrap.js") + Environment.NewLine +
Url.Content("/Scripts/respond.js") + Environment.NewLine +
"NETWORK:" + Environment.NewLine +
"*" + Environment.NewLine;
using (StreamWriter sw = new StreamWriter(path))
Which can be placed inside the Home/Index controller. Then add the path to the _Layout file like this:
<html lang="en" manifest="/Manifest/offline.manifest">
You will need to add write permissions to this directory for the IIS account so it can delete and write the file. Now if your build number increments each time you do a build and deploy, the manifest file will be regenerated and the client browser will pick up any changes.
The post Offline Web Applications With MVC appeared first on Don't Believe The Type.