From 8420c74b31a7b2173770ed82041a8215edb8bf58 Mon Sep 17 00:00:00 2001 From: Dennis Reimann Date: Sat, 18 Apr 2020 17:56:05 +0200 Subject: [PATCH] Improve static asset caching Cache static assets for one year and set the correct cache control header. Adds the cache busting version based on file content to asset references to invalidate the cache on change. ([further details on the approach](https://andrewlock.net/adding-cache-control-headers-to-static-files-in-asp-net-core/) and [why one year](https://ashton.codes/set-cache-control-max-age-1-year/)) Most of the changes are the additions of the `asp-append-version="true"` attribute, the main configuration change is in `Startup.cs`. --- BTCPayServer/Hosting/Startup.cs | 13 ++++++++++++- BTCPayServer/Views/Account/LoginWithU2F.cshtml | 2 +- BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml | 6 +++--- BTCPayServer/Views/Apps/UpdatePointOfSale.cshtml | 8 ++++---- .../AppsPublic/Crowdfund/ContributeForm.cshtml | 2 +- .../AppsPublic/Crowdfund/MinimalCrowdfund.cshtml | 2 +- .../Views/AppsPublic/ViewCrowdfund.cshtml | 8 ++++---- .../Views/AppsPublic/ViewPointOfSale.cshtml | 14 +++++++------- BTCPayServer/Views/Error/404.cshtml | 2 +- BTCPayServer/Views/Error/429.cshtml | 2 +- BTCPayServer/Views/Error/500.cshtml | 2 +- BTCPayServer/Views/Error/_LayoutError.cshtml | 6 ++++-- BTCPayServer/Views/Home/BitpayTranslator.cshtml | 2 +- BTCPayServer/Views/Home/Home.cshtml | 16 ++++++++-------- BTCPayServer/Views/Invoice/Checkout-Body.cshtml | 6 +++--- BTCPayServer/Views/Invoice/Checkout.cshtml | 4 ++-- .../Views/Invoice/CheckoutNoScript.cshtml | 2 +- BTCPayServer/Views/Invoice/ListInvoices.cshtml | 2 +- BTCPayServer/Views/Manage/AddU2FDevice.cshtml | 2 +- .../Views/Manage/EnableAuthenticator.cshtml | 2 +- .../PaymentRequest/EditPaymentRequest.cshtml | 6 +++--- .../PaymentRequest/ViewPaymentRequest.cshtml | 8 ++++---- .../ShowLightningNodeInfo.cshtml | 12 ++++++------ .../Views/Server/CLightningRestServices.cshtml | 4 ++-- .../Views/Server/LightningWalletServices.cshtml | 2 +- BTCPayServer/Views/Server/LndServices.cshtml | 10 +++++----- BTCPayServer/Views/Server/P2PService.cshtml | 6 +++--- BTCPayServer/Views/Server/RPCService.cshtml | 4 ++-- BTCPayServer/Views/Server/Services.cshtml | 2 +- BTCPayServer/Views/Shared/Header.cshtml | 12 ++++++------ BTCPayServer/Views/Shared/SyncModal.cshtml | 2 +- .../Views/Shared/_BTCPaySupporters.cshtml | 12 ++++++------ BTCPayServer/Views/Shared/_Layout.cshtml | 2 +- BTCPayServer/Views/Shared/_LayoutWelcome.cshtml | 6 +++--- .../Shared/_ValidationScriptsPartial.cshtml | 2 +- .../Views/Stores/AddDerivationScheme.cshtml | 8 ++++---- BTCPayServer/Views/Stores/PayButton.cshtml | 12 ++++++------ BTCPayServer/Views/Wallets/CoinSelection.cshtml | 2 +- .../Views/Wallets/WalletCameraScanner.cshtml | 2 +- BTCPayServer/Views/Wallets/WalletPSBT.cshtml | 4 ++-- BTCPayServer/Views/Wallets/WalletReceive.cshtml | 2 +- BTCPayServer/Views/Wallets/WalletSend.cshtml | 2 +- .../Views/Wallets/WalletSendLedger.cshtml | 4 ++-- .../Views/Wallets/WalletSendVault.cshtml | 4 ++-- BTCPayServer/wwwroot/_bootstrap_kitchensink.html | 4 ++-- 45 files changed, 125 insertions(+), 112 deletions(-) diff --git a/BTCPayServer/Hosting/Startup.cs b/BTCPayServer/Hosting/Startup.cs index 55d347db1..b797bfc97 100644 --- a/BTCPayServer/Hosting/Startup.cs +++ b/BTCPayServer/Hosting/Startup.cs @@ -18,6 +18,7 @@ using System.IO; using Microsoft.Extensions.DependencyInjection.Extensions; using BTCPayServer.Security; using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.Net.Http.Headers; using System.Net; using BTCPayServer.PaymentRequest; using BTCPayServer.Services.Apps; @@ -189,7 +190,17 @@ namespace BTCPayServer.Hosting app.UseRouting(); app.UseCors(); - app.UseStaticFiles(); + app.UseStaticFiles(new StaticFileOptions + { + OnPrepareResponse = ctx => + { + // Cache static assets for one year, set asp-append-version="true" on references to update on change. + // https://andrewlock.net/adding-cache-control-headers-to-static-files-in-asp-net-core/ + const int durationInSeconds = 60 * 60 * 24 * 365; + ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + durationInSeconds; + } + }); + app.UseProviderStorage(options); app.UseAuthentication(); app.UseAuthorization(); diff --git a/BTCPayServer/Views/Account/LoginWithU2F.cshtml b/BTCPayServer/Views/Account/LoginWithU2F.cshtml index 97949f43d..682854b98 100644 --- a/BTCPayServer/Views/Account/LoginWithU2F.cshtml +++ b/BTCPayServer/Views/Account/LoginWithU2F.cshtml @@ -29,7 +29,7 @@ - + - - + + + + + - - + + } diff --git a/BTCPayServer/Views/AppsPublic/Crowdfund/ContributeForm.cshtml b/BTCPayServer/Views/AppsPublic/Crowdfund/ContributeForm.cshtml index 74f45b18a..efc6329b5 100644 --- a/BTCPayServer/Views/AppsPublic/Crowdfund/ContributeForm.cshtml +++ b/BTCPayServer/Views/AppsPublic/Crowdfund/ContributeForm.cshtml @@ -10,7 +10,7 @@ } @if (!string.IsNullOrEmpty(item.Image)) { - + }
diff --git a/BTCPayServer/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml b/BTCPayServer/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml index 1ccfa9d83..c1731b2d7 100644 --- a/BTCPayServer/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml +++ b/BTCPayServer/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml @@ -8,7 +8,7 @@
@if (!string.IsNullOrEmpty(Model.MainImageUrl)) { - + }

diff --git a/BTCPayServer/Views/AppsPublic/ViewCrowdfund.cshtml b/BTCPayServer/Views/AppsPublic/ViewCrowdfund.cshtml index 9ab010371..44ebe3a40 100644 --- a/BTCPayServer/Views/AppsPublic/ViewCrowdfund.cshtml +++ b/BTCPayServer/Views/AppsPublic/ViewCrowdfund.cshtml @@ -24,13 +24,13 @@ - - + + @*We need to make sure btcpay.js is not bundled, else it will not work if there is a RootPath*@ - + } - + @if (!string.IsNullOrEmpty(Model.EmbeddedCSS)) { @Safe.Raw($""); diff --git a/BTCPayServer/Views/AppsPublic/ViewPointOfSale.cshtml b/BTCPayServer/Views/AppsPublic/ViewPointOfSale.cshtml index 3a5446b2c..5e424f936 100644 --- a/BTCPayServer/Views/AppsPublic/ViewPointOfSale.cshtml +++ b/BTCPayServer/Views/AppsPublic/ViewPointOfSale.cshtml @@ -21,21 +21,21 @@ - - + + @if (Model.CustomCSSLink != null) { } - + @if (Model.EnableShoppingCart) { - + - + }