From 9c970e4e36909d3f66a405eedf77db4158938688 Mon Sep 17 00:00:00 2001 From: Rick Butterfield Date: Thu, 8 Feb 2024 11:15:03 +0000 Subject: [PATCH] Allow running the sustainability check on headless sites, where `UmbracoApplicationUrl` may be different to the frontend URL --- .../ContentApps/SustainabilityContentApp.cs | 6 ------ .../Services/SustainabilityService.cs | 11 ++++++++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Community.Sustainability/ContentApps/SustainabilityContentApp.cs b/src/Umbraco.Community.Sustainability/ContentApps/SustainabilityContentApp.cs index 549ca61..dac6ced 100644 --- a/src/Umbraco.Community.Sustainability/ContentApps/SustainabilityContentApp.cs +++ b/src/Umbraco.Community.Sustainability/ContentApps/SustainabilityContentApp.cs @@ -12,12 +12,6 @@ public class SustainabilityContentApp : IContentAppFactory if (!(source is IContent)) return null; - var content = (IContent)source; - - // Only show app on content items with template - if (content.TemplateId is null) - return null; - return new ContentApp { Alias = "sustainabilityCheck", diff --git a/src/Umbraco.Community.Sustainability/Services/SustainabilityService.cs b/src/Umbraco.Community.Sustainability/Services/SustainabilityService.cs index ef09635..052fb10 100644 --- a/src/Umbraco.Community.Sustainability/Services/SustainabilityService.cs +++ b/src/Umbraco.Community.Sustainability/Services/SustainabilityService.cs @@ -1,6 +1,8 @@ using System.Reflection; using System.Text.Json; +using Microsoft.Extensions.Options; using Microsoft.Playwright; +using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Community.Sustainability.Models; namespace Umbraco.Community.Sustainability.Services @@ -12,6 +14,13 @@ public interface ISustainabilityService public class SustainabilityService : ISustainabilityService { + private readonly WebRoutingSettings _webRoutingSettings; + + public SustainabilityService(IOptionsMonitor webRoutingSettings) + { + _webRoutingSettings = webRoutingSettings.CurrentValue; + } + public async Task GetSustainabilityData(string url) { using var playwright = await Playwright.CreateAsync(); @@ -25,7 +34,7 @@ public async Task GetSustainabilityData(string url) // Add our script to report data await page.AddScriptTagAsync(new PageAddScriptTagOptions() { - Url = "/App_Plugins/Umbraco.Community.Sustainability/js/resource-checker.js", + Url = $"{_webRoutingSettings.UmbracoApplicationUrl}/App_Plugins/Umbraco.Community.Sustainability/js/resource-checker.js", Type = "module" });