Skip to content

Commit

Permalink
`
Browse files Browse the repository at this point in the history
Refactor output cache checks for edit/preview mode

- Added necessary using directives in `KenticoFeatureExtensions.cs` for new implementations.
- Created `XperienceCommunity.OutputCache.Extensions` namespace with `KenticoFeatureExtensions` class, introducing `InPreview` and `InEditMode` methods to extend `HttpContext`.
- Removed unused `Kentico.Web.Mvc` and added `XperienceCommunity.OutputCache.Extensions` using directive in `XperienceOutputCachePolicy.cs`.
- Refactored `XperienceOutputCachePolicy` to use new extension methods for preview/edit mode checks, enhancing readability.
- Fixed formatting in `XperienceCommunity.OutputCache.csproj` for `PackageReference`.
  • Loading branch information
bluemodus-brandon committed Jul 11, 2024
1 parent a55d446 commit 1bf12aa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Kentico.Content.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using Kentico.Web.Mvc;
using Microsoft.AspNetCore.Http;

namespace XperienceCommunity.OutputCache.Extensions
{
internal static class KenticoFeatureExtensions
{
/// <summary>
/// Checks if the current request is in preview mode.
/// </summary>
/// <param name="context">The HttpContext object.</param>
/// <returns>True if the request is in preview mode, otherwise false.</returns>
internal static bool InPreview(this HttpContext context)
{
return context?.Kentico()?.Preview()?.Enabled ?? false;
}

/// <summary>
/// Checks if the current request is in edit mode.
/// </summary>
/// <param name="context">The HttpContext object.</param>
/// <returns>True if the request is in edit mode, otherwise false.</returns>
internal static bool InEditMode(this HttpContext context)
{
return context?.Kentico()?.PageBuilder()?.EditMode ?? false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using CMS.ContactManagement;
using Kentico.Content.Web.Mvc;
using Kentico.Web.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.Extensions.Primitives;
using XperienceCommunity.OutputCache.Extensions;

namespace XperienceCommunity.OutputCache.Policies
{
Expand Down Expand Up @@ -84,7 +84,7 @@ private static bool AttemptOutputCaching(OutputCacheContext context)
return false;
}

if (context.HttpContext.Kentico().Preview().Enabled)
if (context.HttpContext.InPreview()|| context.HttpContext.InEditMode())
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Kentico.Xperience.WebApp" >
<PackageReference Include="Kentico.Xperience.WebApp">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
Expand Down

0 comments on commit 1bf12aa

Please sign in to comment.