Skip to content

Commit d3efcdc

Browse files
v1.0.11
1 parent 5268a7b commit d3efcdc

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

src/Umbraco.Community.Sustainability/Controllers/SustainabilityController.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,23 @@ public async Task<IActionResult> CheckPage([FromQuery] Guid pageKey)
103103
return Ok("Page not found");
104104
}
105105

106+
var nodeUrl = contentItem.Url(mode: UrlMode.Absolute);
107+
106108
var applicationUri = HttpContext.Request.GetApplicationUri(_webRoutingSettings);
107-
var url = contentItem.Url(mode: UrlMode.Absolute);
108-
var sustainabilityData = await _sustainabilityService.GetSustainabilityData(url, applicationUri.AbsoluteUri);
109+
var hostWithSchemeAndPort = $"{applicationUri.Scheme}://{applicationUri.Authority}";
110+
111+
#if DEBUG
112+
nodeUrl = "https://rickbutterfield.dev/";
113+
#endif
114+
115+
var sustainabilityData = await _sustainabilityService.GetSustainabilityData(nodeUrl, hostWithSchemeAndPort);
109116

110117
return Ok(sustainabilityData);
111118
}
112119

113120
[HttpPost]
114121
public async Task<IActionResult> SavePageData([FromQuery] Guid pageKey, [FromBody] SustainabilityResponse data)
115122
{
116-
if (data.TotalSize == 0)
117-
{
118-
return Ok("Missing data to update");
119-
}
120-
121123
var pageMetric = new PageMetric()
122124
{
123125
NodeKey = pageKey,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Http;
2+
3+
namespace Umbraco.Community.Sustainability
4+
{
5+
public class CorsMiddleware
6+
{
7+
private readonly RequestDelegate _next;
8+
9+
public CorsMiddleware(RequestDelegate next)
10+
{
11+
_next = next;
12+
}
13+
14+
public async Task InvokeAsync(HttpContext context)
15+
{
16+
if (context.Request.Path.Value?.StartsWith("/App_Plugins/UmbracoCommunitySustainability/") == true)
17+
{
18+
context.Response.Headers.Append("Access-Control-Allow-Origin", "*");
19+
context.Response.Headers.Append("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
20+
context.Response.Headers.Append("Access-Control-Allow-Headers", "Content-Type, Authorization");
21+
}
22+
23+
await _next(context);
24+
}
25+
}
26+
}

src/Umbraco.Community.Sustainability/Services/SustainabilityService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
using System.Text.Json;
22
using Microsoft.Playwright;
33
using Umbraco.Community.Sustainability.Models;
4+
using Umbraco.Extensions;
45

56
namespace Umbraco.Community.Sustainability.Services
67
{
78
public interface ISustainabilityService
89
{
9-
Task<SustainabilityResponse> GetSustainabilityData(string url, string applicationUrl = "");
10+
public Task<SustainabilityResponse> GetSustainabilityData(string url, string applicationUrl = "");
1011
}
1112

1213
public class SustainabilityService : ISustainabilityService
1314
{
1415
public async Task<SustainabilityResponse> GetSustainabilityData(string url, string applicationUrl = "")
1516
{
1617
using var playwright = await Playwright.CreateAsync();
17-
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() {
18-
Headless = true,
19-
Args = new[] { "--disable-web-security" }
18+
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions()
19+
{
20+
Headless = true
2021
});
2122

2223
var baseUri = new Uri(url);
2324
var context = await browser.NewContextAsync(new()
2425
{
25-
BypassCSP = true,
26-
IgnoreHTTPSErrors = true
26+
BypassCSP = true
2727
});
2828

2929
var page = await context.NewPageAsync();
@@ -40,7 +40,7 @@ public async Task<SustainabilityResponse> GetSustainabilityData(string url, stri
4040

4141
try
4242
{
43-
string scriptUrl = string.Concat(applicationUrl, "App_Plugins/UmbracoCommunitySustainability/js/resource-checker.js");
43+
string scriptUrl = string.Concat(applicationUrl.EnsureEndsWith('/'), "App_Plugins/UmbracoCommunitySustainability/js/resource-checker.js");
4444

4545
var scriptResponse = await page.EvaluateAsync($@"() => {{
4646
import('{scriptUrl}')

src/Umbraco.Community.Sustainability/SustainabilityComposer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using Microsoft.AspNetCore.Builder;
12
using Microsoft.Extensions.DependencyInjection;
23
using Umbraco.Cms.Core.Composing;
34
using Umbraco.Cms.Core.DependencyInjection;
45
using Umbraco.Cms.Core.Notifications;
6+
using Umbraco.Cms.Web.Common.ApplicationBuilder;
57
using Umbraco.Community.Sustainability.ContentApps;
68
using Umbraco.Community.Sustainability.Notifications;
79
using Umbraco.Community.Sustainability.Sections;
@@ -47,6 +49,14 @@ public void Compose(IUmbracoBuilder builder)
4749

4850
builder.Services.AddScoped<IPageMetricService, PageMetricService>();
4951
builder.Services.AddSingleton<ISustainabilityService, SustainabilityService>();
52+
53+
builder.Services.Configure<UmbracoPipelineOptions>(options =>
54+
{
55+
options.AddFilter(new UmbracoPipelineFilter(nameof(CorsMiddleware))
56+
{
57+
PrePipeline = prePipeline => prePipeline.UseMiddleware<CorsMiddleware>()
58+
});
59+
});
5060
}
5161
}
5262
}

src/Umbraco.Community.Sustainability/Umbraco.Community.Sustainability.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<RootNamespace>Umbraco.Community.Sustainability</RootNamespace>
1717
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1818

19-
<VersionPrefix>1.0.10</VersionPrefix>
19+
<VersionPrefix>1.0.11</VersionPrefix>
2020
<VersionSuffix></VersionSuffix>
2121
<Authors>Umbraco Sustainability Community Team</Authors>
2222
<Copyright>$([System.DateTime]::UtcNow.ToString(`yyyy`)) © Umbraco Sustainability Community Team</Copyright>
@@ -30,7 +30,7 @@
3030
</PropertyGroup>
3131

3232
<ItemGroup>
33-
<PackageReference Include="Microsoft.Playwright" Version="1.51.0" />
33+
<PackageReference Include="Microsoft.Playwright" Version="1.52.0" />
3434
</ItemGroup>
3535

3636
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">

0 commit comments

Comments
 (0)