Giter VIP home page Giter VIP logo

versionedassets's Introduction

ASP.NET Core - VersionedAssets

VersionedAssets is middleware and taghelper for easier integration with CDN configured for origin pull. TagHelper creates asset urls with content version hash burned in url (for cache busting). Middleware handles requests comming from CDN and sets caching headers acordingly.

Following example assumes your CDN is configured with origin pull to base url http://[your.site]/static.

TagHelper generated url: http://[your.cdn]/[version-hash]/bundles/app.js

Request from cdn are pulling from urls: http://[your.site]/static/[version-hash]/bundles/app.js

Startup.cs configuration

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging();
        services.AddMvc();     
        services.AddVersionedAssets();       
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(LogLevel.Debug);

        // respond to cdn origin pull asset requests in the form /static/[hash]/* 
        app.UseVersionedAssets()
            .WithUrlPrefix("//[your.cdn]")

        app.UseMvcWithDefaultRoute();
    }
}

View usage

@addTagHelper "*, AspNetCore.VersionedAssets"
<!DOCTYPE html>
<html lang="cs">
<head>
  <meta charset="UTF-8">
  <title>SPA</title>
</head>
<body>
  <script src="~/bundles/app.js" asset-version="FileVersion"></script>
</body>
</html>

Asset version mode

There are two modes how to specify asset-version in tag helper:

  • FileVersion - [version-hash] is based on file sha2 hash
  • GlobalVersion - [version-hash] is static value for all assets configured in Startup.cs. It might be assembly build time, repository git hash... just something that changes for every deployment

Global version Startup.cs configuration example:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddVersionedAssets(o =>  { 
            o.GlobalVersion = "xxx"
        });       
    }
}

Usage without CDN (cache busting only)

It's also possible to use this without CDN for cache busting only.

When no explicit url (CDN) prefix is set VersionedAssets works in local mode with /static prefix. TagHelper generates url like /static/[version-hash]/bundles/app.js that are served with registered middleware.

versionedassets's People

Contributors

dolly22 avatar

Stargazers

Denny Weiß avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.