liveBook · Manning

CDN

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

A content delivery network is a distributed service that delivers web assets from geographically or topologically proximate servers.

A content delivery network (CDN) distributes static assets so they can be served without every request reaching the application server. In an ASP.NET Core layout, a script or link element can point to a CDN URL while a tag helper arranges a local fallback. That matters because a CDN may be unavailable, and cached assets may lag behind an application deployment. section 26.4.1 section 26.4.2

Configure the fallback according to the asset type:

  • JavaScript: set script src to the CDN URL, asp-fallback-src to a local file, and asp-fallback-test to an expression that is true when the library loaded. For jQuery, the test is window.jQuery. section 26.4.1
  • CSS: set link href to the CDN URL and asp-fallback-href to a local stylesheet. Supply a class, property, and expected value for the load test; the Bootstrap example tests whether .btn has display: inline-block. section 26.4.2

A JavaScript fallback in a Razor layout

In Views/Shared/_SimpleLayout.cshtml, a script pointing to CDNJS jQuery 3.6.0 uses asp-fallback-src="/lib/jquery/jquery.min.js" and asp-fallback-test="window.jQuery". The generated HTML includes the CDN script followed by a test that writes a local script element if jQuery is absent. section 26.4.1

Listing 26.15 Using a CDN for CSS in the Views/Shared/_SimpleLayout.cshtml file
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.3/
css/bootstrap.min.css"
        asp-fallback-href="/lib/bootstrap/css/bootstrap.min.css"
        asp-fallback-test-class="btn"
        asp-fallback-test-property="display"
        asp-fallback-test-value="inline-block"
        rel="stylesheet" />
</head>
<body>    
    <div class="m-2">
        @RenderBody()
    </div>
</body>
</html>

The complete layout listing shows the CDNJS Bootstrap URL, local CSS fallback, and all three attributes needed for its computed-style test. section 26.4.2

Fallback and cache busting solve different problems. Fallback handles an asset that did not load from the CDN. Cache busting addresses stale static files: asp-append-version adds a content-derived version query string to a local asset URL, changing it when the file changes. section 26.4.1 section 26.4.2

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

How deep it goes
Works through tag-helper attributes and generated HTML for two fallback tests; does not cover deploying a CDN.
Examples
Configures CDNJS jQuery 3.6.0 with a local script and CDNJS Bootstrap 5.2.3 with a computed-style-tested local stylesheet.
What it assumes
Assumes familiarity with Razor layouts and script and link elements; supplies the attributes needed for each fallback.
What you can do after
Operate/configure it by adding CDN URLs and local fallbacks to Razor layouts, then inspecting generated HTML to check the tests.
How it connects
Connects CDNs to static-file caching, cache busting, ASP.NET Core tag helpers, and JavaScript and CSS fallback tests.

Also explained in

Compare how 4 books explain CDN →

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.