Skip to content

Commit

Permalink
feat: change detection of rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
skttl committed Jul 1, 2023
1 parent 9927935 commit 6d6ab68
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/Our.Umbraco.FullTextSearch/FullTextSearchConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public static class FullTextSearchConstants
{
public const string HttpClientFactoryNamedClientName = "full-text-search";
public const string HttpClientRequestHeaderName = "X-Umbraco-FullTextSearch";
}
20 changes: 15 additions & 5 deletions src/Our.Umbraco.FullTextSearch/Helpers/FullTextSearchHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Our.Umbraco.FullTextSearch.Interfaces;
using Our.Umbraco.FullTextSearch.Models;
using Our.Umbraco.FullTextSearch.Options;
using System;
using Umbraco.Extensions;

namespace Our.Umbraco.FullTextSearch.Helpers
Expand All @@ -24,14 +26,22 @@ IOptions<FullTextSearchOptions> options
_options = options.Value;
}
/// <summary>
/// Check whether the current page is being rendered by the indexer
/// Check whether the current page is being rendered by the Renderer
/// </summary>
/// <returns>true if being indexed</returns>
public bool IsIndexingActive()
/// <returns>true if being rendered by the Renderer</returns>
public bool IsRenderingActive()
{
var searchActiveStringName = _options.IndexingActiveKey;
return _httpContextAccessor.GetRequiredHttpContext().Request.Headers.TryGetValue(FullTextSearchConstants.HttpClientRequestHeaderName, out StringValues requestHeader) && requestHeader == _options.RenderingActiveKey;
}

return !searchActiveStringName.IsNullOrWhiteSpace() && _httpContextAccessor.GetRequiredHttpContext().Items[searchActiveStringName] != null;
/// <summary>
/// Check whether the current page is being rendered by the Renderer
/// </summary>
/// <returns>true if being rendered by the Renderer</returns>
[Obsolete("Use IsRenderingActive")]
public bool IsIndexingActive()
{
return IsRenderingActive();
}

/// <summary>
Expand Down
53 changes: 28 additions & 25 deletions src/Our.Umbraco.FullTextSearch/Options/FullTextSearchOptions.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace Our.Umbraco.FullTextSearch.Options
namespace Our.Umbraco.FullTextSearch.Options;

public class FullTextSearchOptions
{
public class FullTextSearchOptions
{
[JsonProperty("enabled")]
public bool Enabled { get; set; } = true;
[JsonProperty("defaultTitleField")]
public string DefaultTitleField { get; set; } = "nodeName";
[JsonProperty("indexingActiveKey")]
public string IndexingActiveKey { get; set; } = "FullTextIndexingActive";
[JsonProperty("disallowedContentTypeAliases")]
public List<string> DisallowedContentTypeAliases { get; set; } = new List<string>();
[JsonProperty("disallowedPropertyAliases")]
public List<string> DisallowedPropertyAliases { get; set; } = new List<string>();
[JsonProperty("xPathsToRemove")]
public List<string> XPathsToRemove { get; set; } = new List<string>();
[JsonProperty("fullTextContentField")]
public string FullTextContentField { get; set; } = "FullTextContent";
[JsonProperty("fullTextPathField")]
public string FullTextPathField { get; set; } = "FullTextPath";
[JsonProperty("enabled")]
public bool Enabled { get; set; } = true;
[JsonProperty("defaultTitleField")]
public string DefaultTitleField { get; set; } = "nodeName";
[Obsolete("Use RenderingActiveKey instead")]
[JsonProperty("indexingActiveKey")]
public string IndexingActiveKey { get; set; } = "FullTextRenderingActive";
[JsonProperty("renderingActiveKey")]
public string RenderingActiveKey { get; set; } = "FullTextRenderingActive";
[JsonProperty("disallowedContentTypeAliases")]
public List<string> DisallowedContentTypeAliases { get; set; } = new List<string>();
[JsonProperty("disallowedPropertyAliases")]
public List<string> DisallowedPropertyAliases { get; set; } = new List<string>();
[JsonProperty("xPathsToRemove")]
public List<string> XPathsToRemove { get; set; } = new List<string>();
[JsonProperty("fullTextContentField")]
public string FullTextContentField { get; set; } = "FullTextContent";
[JsonProperty("fullTextPathField")]
public string FullTextPathField { get; set; } = "FullTextPath";

/// <summary>
/// Pattern to use for highlighting text in search result html. Example: &lt;b&gt;{0}&lt;/b&gt;
/// </summary>
[JsonProperty("highlightPattern")]
public string HighlightPattern { get; set; } = "<b>{0}</b>";
}
/// <summary>
/// Pattern to use for highlighting text in search result html. Example: &lt;b&gt;{0}&lt;/b&gt;
/// </summary>
[JsonProperty("highlightPattern")]
public string HighlightPattern { get; set; } = "<b>{0}</b>";
}
20 changes: 13 additions & 7 deletions src/Our.Umbraco.FullTextSearch/Rendering/HttpPageRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Our.Umbraco.FullTextSearch.Interfaces;
using Our.Umbraco.FullTextSearch.Options;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Our.Umbraco.FullTextSearch.Interfaces;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Extensions;

Expand All @@ -14,13 +16,16 @@ namespace Our.Umbraco.FullTextSearch.Rendering;
/// </summary>
public class HttpPageRenderer : IPageRenderer
{
private readonly FullTextSearchOptions _options;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<HttpPageRenderer> _logger;

public HttpPageRenderer(
IHttpClientFactory httpClientFactory,
IOptions<FullTextSearchOptions> options,
IHttpClientFactory httpClientFactory,
ILogger<HttpPageRenderer> logger)
{
_options = options.Value;
_httpClientFactory = httpClientFactory;
_logger = logger;
}
Expand All @@ -35,22 +40,23 @@ public virtual async Task<string> Render(IPublishedContent publishedContent, Pub
// if the named client is not registered during startup it will fallback so we never need to register if inside the package.

var httpClient = _httpClientFactory.CreateClient(FullTextSearchConstants.HttpClientFactoryNamedClientName);
httpClient.DefaultRequestHeaders.Add(FullTextSearchConstants.HttpClientRequestHeaderName, _options.RenderingActiveKey);
var result = await httpClient.GetAsync(publishedPageUrl);

string fullHtml = string.Empty;

// If the response is not status OK (like a 40X or 30X) we don't want to index the content.
if (result.StatusCode == HttpStatusCode.OK)
{
fullHtml = result.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
fullHtml = await result.Content.ReadAsStringAsync();
}

return fullHtml;

}
catch (Exception e)
{
_logger.LogError(e, "Error in http-request for full text indexing of page {nodeId}, tried to fetch {url}",publishedContent.Id, publishedPageUrl);
_logger.LogError(e, "Error in http-request for full text indexing of page {nodeId}, tried to fetch {url}", publishedContent.Id, publishedPageUrl);
}

return string.Empty;
Expand Down
14 changes: 7 additions & 7 deletions src/Our.Umbraco.FullTextSearch/Rendering/RazorPageRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Our.Umbraco.FullTextSearch.Interfaces;
using Our.Umbraco.FullTextSearch.Options;
using System.Threading.Tasks;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Templates;
using Umbraco.Extensions;
Expand All @@ -25,9 +25,9 @@ public class RazorPageRenderer : IPageRenderer

public RazorPageRenderer(

IUmbracoComponentRenderer umbracoComponentRenderer,
IVariationContextAccessor variationContextAccessor,
IHttpContextAccessor httpContextAccessor,
IUmbracoComponentRenderer umbracoComponentRenderer,
IVariationContextAccessor variationContextAccessor,
IHttpContextAccessor httpContextAccessor,
IOptions<FullTextSearchOptions> options)
{
_umbracoComponentRenderer = umbracoComponentRenderer;
Expand All @@ -41,12 +41,12 @@ public virtual async Task<string> Render(IPublishedContent publishedContent, Pub
if (!culture.Culture.IsNullOrWhiteSpace())
_variationContextAccessor.VariationContext = new VariationContext(culture.Culture);

_httpContextAccessor.HttpContext?.Items.Add(_options.IndexingActiveKey, "1");
_httpContextAccessor.HttpContext?.Request.Headers.Add(FullTextSearchConstants.HttpClientRequestHeaderName, _options.RenderingActiveKey);

// todo do we need the wrapping template?
var fullHtml = await _umbracoComponentRenderer.RenderTemplateAsync(publishedContent.Id, publishedContent.TemplateId);

_httpContextAccessor.HttpContext?.Items.Remove(_options.IndexingActiveKey);
_httpContextAccessor.HttpContext?.Request.Headers.Remove(FullTextSearchConstants.HttpClientRequestHeaderName);

return fullHtml.ToString();
}
Expand Down

0 comments on commit 6d6ab68

Please sign in to comment.