It is normal for ASP.Net applications to use bundles to reduce http calls. It all works till the time CDN comes into the landscape. CDN (Content Delivery Network) uses a special approach to deliver contents from world wide distributed servers which we can envision as served from the edge of the network. That way so many requests get served from CDN server without coming to our web server. It is nice theory about CDN. What is between ASP.Net bundling and CDN?
CDN normally cache static assets. Out of the box, they cache well known static assets such as html, css, js files. Yeah, image file formats too, if we configure. If we had bundled static assets using ASP.Net bundling mechanism which gets a different URL than just js files, those will not be seen by CDN as static assets. Hence no caching.
This is applicable if CDN is placed in between. Akamai provides such a service where we don't need to change our code when we introduce CDN. CDN servers cache when they get content from the backend web server. Normally when we introduce CDN, the domain has to change to serve from CDN. That has added benefit too since it increase the parallel request limit to same origin. If we want to integrate CDN via separate domain, better forget about ASP.Net bundling.
Here lets see how to make pass through CDNs work with ASP.Net bundling since they don't introduce new CDN URL.
How can we make CDN friend with ASP.Net bundles
Approach 1 - Don't use bundling
Sounds easy but if the ASP.Net application is designed as product which is hosted by individual customer, every consumer may not have CDN capability. So need bundling. For hosted applications ie there is only one instance of application in the world, yes we can try this. But still without http2, it would cause performance degradation as previous http versions make different connections for each file / resource request. If the application has angular or any other SPA frameworks used, the number of files may be large.
Approach 2 - Have CDN cache the ASP.Net bundle URL
When we bundle, ASP.Net provides a new URL instead of all the files inside the bundle. From browser we will see only one bundle URL request instead of individual resources in the bundle. That URL has a predefined format. If the CDN supports custom RegEx kind of way to specify the URL formats to cache, it works without much efforts.
The RegEx for recognizing ASP.Net bundle URL format goes as follows.
/\?v=[a-zA-Z0-9_-]{44}$/
This works only if there are no other API or dynamic resource URL ending with ?v={44 letter value}
Approach 3 - Embrace SPA with JS modules & forget server side rendering
Instead of server side rendering done by ASP.Net, embrace the industry trend SPA (Singe Page Application) with JavaScript module system such as Webpack. Webpack can produce static bundles which can be referred in application as a normal file.
Recommended
If there is enough budget and time, embrace SPA else use the RegEx and stay with ASP.Net bundling and CDN.
I don't have any association with Akamai except using in projects. This post is not intended to promote any of their products.
I don't have any association with Akamai except using in projects. This post is not intended to promote any of their products.
No comments:
Post a Comment