/ETag

An Umbraco package that adds ETag headers to Umbraco content pages

Primary LanguageCSSMIT LicenseMIT

Umbraco ETag

Downloads Nuget (with prereleases) GitHub

Boost your website performance with ETag headers. An ETag header will increase cachability of your content and makes browsers reuse cached content if it hasn't changed.

Getting Started

You can use the ETag package in two different ways:

  1. Opt-in on specific Umbraco pages:
    using Infocaster.Umbraco.ETag
    
    [ETag]
    public class HomeController : RenderController
    {
        
    }
    Use this method if you only want to use the ETag on specific pages
  2. On all Umbraco pages:
    public class Startup
    {
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
     	   app.UseUmbraco()
                 .WithMiddleware(u =>
                 {
                     u.UseBackOffice();
                     u.UseWebsite();
    
                     u.UseETag();
                 })
        }
    }
    Use this method if you want to use ETag on all pages

Do not use both methods at the same time

Customizing the ETag

By default, the ETag is calculated with the following data:

  • The last update date of the current page
  • The last update date of the current page's template
  • The last update date of every ancestor of the current page
  • The current hour (that is: the ETag changes at least once every hour)

If you use related content or content pickers in your page, then you'll likely need to customize the creation of ETags before it works as expected. In order to do that, you should implement your own IETagFactory like this:

public MyCustomETagFactory : IETagFactory
{
    public string Create(IPublishedRequest request)
    {
        /* Here you might want to:
         *  - Include the update date of your layout model
         *  - Precalculate any related content and include their update dates
         *  - Include any update dates of other dependencies
         *
         * A good way to implement this class for many different document types is by using a strategy pattern:
         * https://refactoring.guru/design-patterns/strategy
         */
        string result = // ... Your calculation to calculate your etag

        return result;
    }
}

Then to overwrite the default implementation, you can create a composer:

[ComposeAfter(typeof(Infocaster.Umbraco.ETag.ETagComposer))]
public MyCustomETagComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.Services.AddUnique<IETagFactory, MyCustomETagFactory>();
    }
}

Contributing

This package is open for contributions. If you want to contribute to the source code, please check out our guide to contributing.
These people have already contributed to this awesome project:

Made with contributors-img.