Skip to content

Commit 3e6f711

Browse files
Merge pull request #20 from Kentico/CM-7484-caching-for-static-content
Fix client caching for static content when Preview is enabled
2 parents c3a4f5f + 0dc00e5 commit 3e6f711

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
#### Fixed
6363

6464
* Updated Kentico package description for NuGet.org.
65+
* Fix client caching for static content when Preview module is enabled.
66+
[#20](https://github.com/Kentico/Mvc/pull/20)
6567

6668
## Kentico.Core
6769

src/Kentico.Content.Web.Mvc/Kentico.Content.Web.Mvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Compile Include="Preview\IPreviewFeature.cs" />
5353
<Compile Include="Preview\PreviewFeature.cs" />
5454
<Compile Include="Preview\PreviewFeatureModule.cs" />
55+
<Compile Include="Preview\PreviewOutputCacheFilter.cs" />
5556
<Compile Include="Properties\AssemblyInfo.cs" />
5657
</ItemGroup>
5758
<ItemGroup>

src/Kentico.Content.Web.Mvc/Preview/PreviewFeatureModule.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Web;
3+
using System.Web.Mvc;
34
using System.Web.Helpers;
45

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

2628

@@ -30,7 +32,6 @@ private void HandleBeginRequest(object sender, EventArgs e)
3032
var context = application.Context;
3133
var relativeFilePath = context.Request.AppRelativeCurrentExecutionFilePath.TrimStart('~');
3234

33-
3435
// Check whether the current request URL contains information about a preview mode
3536
if (HandleVirtualContext(ref relativeFilePath))
3637
{
@@ -51,11 +52,6 @@ private void HandleBeginRequest(object sender, EventArgs e)
5152
// Remove preview mode information from the request URL
5253
context.RewritePath("~" + relativeFilePath, context.Request.PathInfo, context.Request.Url.Query.TrimStart('?'));
5354
}
54-
else
55-
{
56-
// Add validation callback for the output cache item to ignore it in a preview mode
57-
context.Response.Cache.AddValidationCallback(ValidateCacheItem, null);
58-
}
5955

6056
var previewFeature = new PreviewFeature();
6157
context.Kentico().SetFeature<IPreviewFeature>(previewFeature);
@@ -89,14 +85,5 @@ private static bool ValidatePreviewGuid(object value)
8985

9086
return query.HasResults();
9187
}
92-
93-
94-
private static void ValidateCacheItem(HttpContext context, object data, ref HttpValidationStatus status)
95-
{
96-
if (context.Kentico().Preview().Enabled)
97-
{
98-
status = HttpValidationStatus.IgnoreThisRequest;
99-
}
100-
}
10188
}
10289
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Web;
2+
using System.Web.Mvc;
3+
4+
using Kentico.Web.Mvc;
5+
6+
namespace Kentico.Content.Web.Mvc
7+
{
8+
/// <summary>
9+
/// Adds validation callback for the output cache item to ignore it in a preview mode.
10+
/// </summary>
11+
internal class PreviewOutputCacheFilter : ActionFilterAttribute
12+
{
13+
public override void OnActionExecuting(ActionExecutingContext filterContext)
14+
{
15+
var context = filterContext.HttpContext;
16+
17+
if (!context.Kentico().Preview().Enabled)
18+
{
19+
context.Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(ValidateCacheItem), null);
20+
}
21+
}
22+
23+
24+
private static void ValidateCacheItem(HttpContext context, object data, ref HttpValidationStatus status)
25+
{
26+
if (context.Kentico().Preview().Enabled)
27+
{
28+
status = HttpValidationStatus.IgnoreThisRequest;
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)