andrewdavey / cassette

Manages .NET web application assets (scripts, css and templates)
http://getcassette.net
MIT License
536 stars 144 forks source link

Addition of CDN management #444

Open cankoluman opened 10 years ago

cankoluman commented 10 years ago

It would be great to get this into the main distribution. I based the changes on the comments in the mailing list. Inherits from 'External Bundles' with local assets and generates CDN url prefixed urls for bundles, adds ability to pre-compress (gzip) a bundle, and rewrites URLs in css files assuming a mirrored deployment of assets to the CDN / S3 cloud. In debug mode, continues to use local assets.

Right now, the changes exist inside the main Cassette distribution - on account of 'internal' scoped items and to reduce code duplication. If internal scoped items are made public, then this could be moved into a separate project.

Usage example:

public void Configure(BundleCollection bundles)
{
    bundles.AddCdnUrlWithLocalAssets<CdnStylesheetBundle>(
        <bundle alias>,
        <CDN Root>,
        <CDN Cache root>,
        new LocalAssetSettings
        {
            // Local assets for use in debug mode.
            Path = <asset path>,
            FallbackCondition = "false"
        },
        bundle =>
        {
            ((CdnStylesheetBundle)bundle).GzipStylesheetBundle(isDebuggingEnabled : bool, gzipInDebug : bool default false);
        }
    );

    bundles.AddCdnUrlWithLocalAssets<CdnScriptBundle>(
        <bundle alias>,
        <CDN Root>,
        <CDN Cache root>,
        new LocalAssetSettings
        {
            Path = "<asset path>",
            FileSearch = new FileSearch
            {
                Pattern = "*.js"
            },
            FallbackCondition = "false"
        },
        bundle =>
        {
            ((CdnScriptBundle)bundle).GzipStylesheetBundle(isDebuggingEnabled : bool, gzipInDebug : bool default false);
        }
    );
}

Cheers.

cankoluman commented 10 years ago

Corrected from the above:

        bundle =>
        {
            ((CdnScriptBundle)bundle).GzipScriptBundle(isDebuggingEnabled : bool, gzipInDebug : bool default false);
        }
kamranayub commented 8 years ago

I have a post on how to handle CDNs in production for local assets as well, using a CdnUrlModifier.http://kamranicus.com/blog/2015/10/10/azure-cdn-cassette/