Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/Docfx.App/PdfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,42 @@ IResult TocPage(string url)

try
{
Uri beforeUri = new(page.Url);
var response = await page.GotoAsync(url.ToString(), new() { WaitUntil = WaitUntilState.DOMContentLoaded });
if (response?.Status is 404)
return null;

if (response is null || !response.Ok)
throw new InvalidOperationException($"Failed to build PDF page [{response?.Status}]: {url}");
bool isSameUrlNavigation = response == null && beforeUri == url;
bool isHashFragmentNavigation = response == null
&& beforeUri.GetLeftPart(UriPartial.Path) == url.GetLeftPart(UriPartial.Path)
&& beforeUri.Fragment != url.Fragment;

try
if (isSameUrlNavigation)
{
// Specified page content is already loaded.
}
else if (isHashFragmentNavigation)
{
await page.AddScriptTagAsync(new() { Content = EnsureHeadingAnchorScript });
await page.WaitForFunctionAsync("!window.docfx || window.docfx.ready");
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);
// Hash fragment navigation inside page. network request is not executed.
await page.WaitForURLAsync(url.ToString());
}
catch (TimeoutException)
else if (response is null || !response.Ok)
{
Logger.LogWarning($"Timeout waiting for page to load, generated PDF page may be incomplete: {url}");
// Goto navigation failed.
throw new InvalidOperationException($"Failed to build PDF page [{response?.Status}]: {url}");
}
else
{
try
{
await page.AddScriptTagAsync(new() { Content = EnsureHeadingAnchorScript });
await page.WaitForFunctionAsync("!window.docfx || window.docfx.ready");
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);
}
catch (TimeoutException)
{
Logger.LogWarning($"Timeout waiting for page to load, generated PDF page may be incomplete: {url}");
}
}

return await page.PdfAsync(new PagePdfOptions
Expand Down
Loading