From 622e5e5ecf4e6fe0d36c6dc3476b627a8ecd012a Mon Sep 17 00:00:00 2001 From: Peter Petrinec Date: Thu, 19 Jan 2017 11:15:35 +0100 Subject: [PATCH 1/2] CM-7484 Fix client caching for static content when Preview is enabled --- .../Kentico.Content.Web.Mvc.csproj | 1 + .../Preview/PreviewFeatureModule.cs | 17 ++-------- .../Preview/PreviewOutputCacheFilter.cs | 32 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 2 +- 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 src/Kentico.Content.Web.Mvc/Preview/PreviewOutputCacheFilter.cs diff --git a/src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj b/src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj index 0d4b3a7..c85f04a 100644 --- a/src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj +++ b/src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj @@ -52,6 +52,7 @@ + diff --git a/src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs b/src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs index 6b23908..9b882a0 100644 --- a/src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs +++ b/src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs @@ -1,5 +1,6 @@ using System; using System.Web; +using System.Web.Mvc; using System.Web.Helpers; using CMS.DocumentEngine; @@ -21,6 +22,7 @@ internal sealed class PreviewFeatureModule : IModule public void Initialize(HttpApplication application) { application.BeginRequest += HandleBeginRequest; + GlobalFilters.Filters.Add(new PreviewOutputCacheFilter()); } @@ -30,7 +32,6 @@ private void HandleBeginRequest(object sender, EventArgs e) var context = application.Context; var relativeFilePath = context.Request.AppRelativeCurrentExecutionFilePath.TrimStart('~'); - // Check whether the current request URL contains information about a preview mode if (HandleVirtualContext(ref relativeFilePath)) { @@ -51,11 +52,6 @@ private void HandleBeginRequest(object sender, EventArgs e) // Remove preview mode information from the request URL context.RewritePath("~" + relativeFilePath, context.Request.PathInfo, context.Request.Url.Query.TrimStart('?')); } - else - { - // Add validation callback for the output cache item to ignore it in a preview mode - context.Response.Cache.AddValidationCallback(ValidateCacheItem, null); - } var previewFeature = new PreviewFeature(); context.Kentico().SetFeature(previewFeature); @@ -89,14 +85,5 @@ private static bool ValidatePreviewGuid(object value) return query.HasResults(); } - - - private static void ValidateCacheItem(HttpContext context, object data, ref HttpValidationStatus status) - { - if (context.Kentico().Preview().Enabled) - { - status = HttpValidationStatus.IgnoreThisRequest; - } - } } } \ No newline at end of file diff --git a/src/Kentico.Content.Web.Mvc/Preview/PreviewOutputCacheFilter.cs b/src/Kentico.Content.Web.Mvc/Preview/PreviewOutputCacheFilter.cs new file mode 100644 index 0000000..c38416a --- /dev/null +++ b/src/Kentico.Content.Web.Mvc/Preview/PreviewOutputCacheFilter.cs @@ -0,0 +1,32 @@ +using System.Web; +using System.Web.Mvc; + +using Kentico.Web.Mvc; + +namespace Kentico.Content.Web.Mvc +{ + /// + /// Adds validation callback for the output cache item to ignore it in a preview mode. + /// + internal class PreviewOutputCacheFilter : ActionFilterAttribute + { + public override void OnActionExecuting(ActionExecutingContext filterContext) + { + var context = filterContext.HttpContext; + + if (!context.Kentico().Preview().Enabled) + { + context.Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(ValidateCacheItem), null); + } + } + + + private static void ValidateCacheItem(HttpContext context, object data, ref HttpValidationStatus status) + { + if (context.Kentico().Preview().Enabled) + { + status = HttpValidationStatus.IgnoreThisRequest; + } + } + } +} diff --git a/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs b/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs index 3fee341..b93c25a 100644 --- a/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs +++ b/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs @@ -14,6 +14,6 @@ [assembly: Guid("430a1add-4583-459f-879c-a22a61e44a3b")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] -[assembly: AssemblyInformationalVersion("1.0.1")] +[assembly: AssemblyInformationalVersion("1.0.2")] [assembly: InternalsVisibleTo("Kentico.Content.Web.Mvc.Tests")] \ No newline at end of file From 0dc00e50646183e0a7a30ec3d68484a8030035cd Mon Sep 17 00:00:00 2001 From: Peter Petrinec Date: Mon, 23 Jan 2017 13:22:00 +0100 Subject: [PATCH 2/2] CM-7484 Update changelog and decrease the assemly version since it hasn't been released yet --- CHANGELOG.md | 2 ++ src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16ee5e2..56fdc1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,8 @@ #### Fixed * Updated Kentico package description for NuGet.org. +* Fix client caching for static content when Preview module is enabled. +[#20](https://github.com/Kentico/Mvc/pull/20) ## Kentico.Core diff --git a/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs b/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs index b93c25a..3fee341 100644 --- a/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs +++ b/src/Kentico.Content.Web.Mvc/Properties/AssemblyInfo.cs @@ -14,6 +14,6 @@ [assembly: Guid("430a1add-4583-459f-879c-a22a61e44a3b")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] -[assembly: AssemblyInformationalVersion("1.0.2")] +[assembly: AssemblyInformationalVersion("1.0.1")] [assembly: InternalsVisibleTo("Kentico.Content.Web.Mvc.Tests")] \ No newline at end of file