https://discord.umbraco.com logo
#social
Title
# social
d

Dave de Moel

12/10/2021, 6:36 PM
Copy code
csharp
app.UseUmbraco()
    .WithCustomMiddleware(u =>
    {
        u.RunPrePipeline();
        u.UseUmbracoCoreMiddleware();
        u.AppBuilder.UseStatusCodePages();
        u.AppBuilder.UseImageSharp();
        u.AppBuilder.UseStaticFiles();
        u.AppBuilder.UseUmbracoPluginsStaticFiles();
        u.AppBuilder.UseRequestLocalization();
        u.AppBuilder.UseRouting();
        u.AppBuilder.UseCors();
        u.AppBuilder.UseResponseCaching();
        u.AppBuilder.Use(async (ctx, next) =>
        {
            if (ctx.Request.Method.Equals(HttpMethod.Get.ToString()))
            {
                if (u.ApplicationServices.GetRequiredService<IOptions<ServerOptions>>().Value.Role == "replica")
                {
                    ctx.Response.Headers[HeaderNames.CacheControl] = "public, max-age=300";
                }
                else
                {
                    ctx.Response.Headers[HeaderNames.CacheControl] = "private";                                
                }
                            
                ctx.Response.Headers[HeaderNames.Vary] = new[] { "Accept-Encoding" };
            }

            if (ctx.RequestServices.GetRequiredService<IUmbracoContextAccessor>()
                .TryGetUmbracoContext(out var umbracoContext))
            {
                var req = umbracoContext.PublishedRequest;
                if (req is { PublishedContent: { } })
                {
                    ctx.Response.Headers[HeaderNames.LastModified] =
                         req.PublishedContent.UpdateDate.ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'");
                }
             }

             await next();
         });
         u.AppBuilder.UseAuthentication();
         u.AppBuilder.UseAuthorization();
         u.AppBuilder.UseRequestLocalization();
         u.AppBuilder.UseSession();
         u.RunPostPipeline();

         u.UseBackOffice();
         u.UseWebsite(); 
        u.UseAzureBlobMediaFileSystem();
})