ASP.NET Core 2: Javascript CDNs and Fallbacks

Content Deployment Networks (CDN) are great for deploying javascipt, CSS and other resources which might be common. jQuery is a great example. When your site uses a CDN that another site your user has used, your user might already have the resource in their cache so your site will load faster.

ASP.NET Core handles these nicely and offers fallback support so if the CDN is down your site won’t go down.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"
        asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
        asp-fallback-test="window.jQuery">
</script>

 

There are 2 new TagHelpers here.

asp-fallback-src: Simply indicates where the backup script is on your system

asp-fallback-test: This is the object that ASP.NET Core will look for. If it’s not found after the load attempt, your fallback will be used.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s