liveBook · Manning

Cache busting

As explained in Pro ASP.NET Core 7, Tenth Edition

A cache-busting value is a changing resource identifier component that causes clients to retrieve a new version instead of using a stale cached file.

Browsers, cache servers, and content delivery networks can serve static files without contacting the application. That saves requests, but a deployment can leave clients using old JavaScript or images alongside newly generated pages. Cache busting changes a resource’s URL when its file changes, so caches can retain unchanged files without serving stale versions indefinitely. section 26.4.1

Version a JavaScript file

In Views/Shared/_SimpleLayout.cshtml, add asp-append-version="true" to the script element: <script asp-src-include="/lib/jquery/**/*.min.js" asp-src-exclude="**.slim.**" asp-append-version="true"></script> The rendered element includes a version query string, such as <script src="/lib/jquery/jquery.min.js?v=_xUj3OJU5yExlq6GSYGSHk7tPXikyn"></script>. The include and exclude attributes choose the file; asp-append-version enables cache busting. section 26.4.1

Version an image

The ImageTagHelper applies the same attribute to an image in the shared layout: <img src="/images/city.png" asp-append-version="true" class="m-2" />. Request http://localhost:5000/home/list and inspect the HTML response. The image URL includes a version checksum, such as /images/city.png?v=KaMNDSZFAJufRcRDpKh0K_IIPNc7E; changes to the file can therefore pass through caches instead of returning the stale image. section 26.5

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains the stale-cache problem and shows generated version URLs, without detailing checksum generation.
Examples
Shows versioned jQuery and skyline-image URLs generated from elements in a shared Razor layout.
What it assumes
Assumes familiarity with static-resource elements and Razor layouts, but introduces the stale-cache problem before showing the attribute.
What you can do after
Apply it via ASP.NET Core tag helpers by setting asp-append-version on script and img elements and inspecting rendered URLs.
How it connects
Connects cache busting to static files, browser caching, content delivery networks, and ASP.NET Core tag helpers.

Also explained in

Compare how 2 books explain cache busting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.