Skip to content

Commit

Permalink
Merge pull request #20 from Kentico/CM-7484-caching-for-static-content
Browse files Browse the repository at this point in the history
Fix client caching for static content when Preview is enabled
  • Loading branch information
KenticoPavelV authored Jan 26, 2017
2 parents c3a4f5f + 0dc00e5 commit 3e6f711
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Compile Include="Preview\IPreviewFeature.cs" />
<Compile Include="Preview\PreviewFeature.cs" />
<Compile Include="Preview\PreviewFeatureModule.cs" />
<Compile Include="Preview\PreviewOutputCacheFilter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
17 changes: 2 additions & 15 deletions src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;

using CMS.DocumentEngine;
Expand All @@ -21,6 +22,7 @@ internal sealed class PreviewFeatureModule : IModule
public void Initialize(HttpApplication application)
{
application.BeginRequest += HandleBeginRequest;
GlobalFilters.Filters.Add(new PreviewOutputCacheFilter());
}


Expand All @@ -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))
{
Expand All @@ -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<IPreviewFeature>(previewFeature);
Expand Down Expand Up @@ -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;
}
}
}
}
32 changes: 32 additions & 0 deletions src/Kentico.Content.Web.Mvc/Preview/PreviewOutputCacheFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Web;
using System.Web.Mvc;

using Kentico.Web.Mvc;

namespace Kentico.Content.Web.Mvc
{
/// <summary>
/// Adds validation callback for the output cache item to ignore it in a preview mode.
/// </summary>
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;
}
}
}
}

0 comments on commit 3e6f711

Please sign in to comment.