Skip to content

Commit 38eaafd

Browse files
Including <head> contents when manifest can't be found via puppeteer.
1 parent 8f9d107 commit 38eaafd

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

apps/pwabuilder/Frontend/package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/pwabuilder/Services/ManifestDetector.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public ManifestDetector(WebStringCache webStringCache, IPuppeteerService puppete
110110
var manifestUrl = manifestUrls.LastOrDefault();
111111
if (manifestUrl == null)
112112
{
113-
logger.LogWarning("Using Puppeteer to find manifest, no manifest links found in the page at {appUrl}.", appUrl);
113+
var headContent = await TryGetHeadContentsFromPuppeteerAsync(puppeteerPage, logger);
114+
logger.LogWarning("Using Puppeteer to find manifest, no manifest links found in the page at {appUrl}. Page <head> content: {pageContent}", appUrl, headContent);
114115
return null;
115116
}
116117

@@ -240,4 +241,32 @@ public ManifestDetector(WebStringCache webStringCache, IPuppeteerService puppete
240241
return null;
241242
}
242243
}
244+
245+
/// <summary>
246+
/// Attempts to get the HTML contents of the head element from a Puppeteer page.
247+
/// </summary>
248+
/// <param name="page">The Puppeteer page to extract head contents from.</param>
249+
/// <param name="logger">The logger to use for logging warnings.</param>
250+
/// <returns>The HTML string of the head element, or null if it doesn't exist or an exception occurs.</returns>
251+
private static async Task<string?> TryGetHeadContentsFromPuppeteerAsync(IPage page, ILogger logger)
252+
{
253+
try
254+
{
255+
var headContent = await page.EvaluateExpressionAsync<string?>("document.head?.outerHTML");
256+
257+
// Ensure head content is reasonable size (under 10k bytes) before returning
258+
if (headContent is not null && System.Text.Encoding.UTF8.GetByteCount(headContent) > 10_000)
259+
{
260+
logger.LogWarning("Head content is too large ({size} bytes), truncating for logging.", System.Text.Encoding.UTF8.GetByteCount(headContent));
261+
return headContent[..Math.Min(headContent.Length, 10_000)] + "... [truncated]";
262+
}
263+
264+
return headContent;
265+
}
266+
catch (Exception error)
267+
{
268+
logger.LogWarning(error, "Error retrieving head contents from Puppeteer page.");
269+
return null;
270+
}
271+
}
243272
}

0 commit comments

Comments
 (0)