0

I am working on porting an app from TWebBrowser to TEdgeBrowser. I am utilizing the latest WebView2Loader from the Nuget site and have seen the same consistent problem with all versions of WebView2Loader across every version going back to January 2021.

The TEdgeBrowser works well, but the first load of any page can take anywhere from 14 seconds to as much as 24 seconds, usually between 14-18 seconds. Once the first page is loaded, any subsequent page loads execute as quickly as you would expect them to in a full browser.

I specify a UserDataFolder and a BrowserExecutableFolder, as well as OnCreateWebViewCompleted,OnNavigationStarting, and OnNavigationCompleted events.

I check for any of the error constants defined in the interface code but the only error that comes back is 'COREWEBVIEW2_WEB_ERROR_STATUS_UNKNOWN', which occurs every time I successfully navigate to a web page.

Does anyone have any insight into what could be causing this delay? I can run my embedded browser next to Microsft Edge and Edge loads quickly as expected, but the TEdgeBrowser always has the initial delay.

Any assistance would be greatly appreciated. enter image description here

unit main;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Winapi.WebView2,
  Winapi.ActiveX,
  Vcl.Edge,
  Vcl.StdCtrls,
  Vcl.ExtCtrls,
  Vcl.OleCtrls,
  Vcl.ComCtrls,
  Vcl.Mask;

type
  TForm2 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Edit1: TEdit;
    moLog: TMemo;
    Splitter1: TSplitter;
    edNavElapsedTime: TLabeledEdit;
    PageControl1: TPageControl;
    tsEB: TTabSheet;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FStart: TDateTime;
    FStop: TDateTime;
    FEB: TEdgeBrowser;
    procedure CreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HResult);
    procedure NavigationCompleted(Sender: TCustomEdgeBrowser; IsSuccess: Boolean; WebErrorStatus: COREWEBVIEW2_WEB_ERROR_STATUS);
    procedure NavigationStarting(Sender: TCustomEdgeBrowser; Args: TNavigationStartingEventArgs);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses
  System.DateUtils;

procedure LogEntry(ALogEntry: UnicodeString);
begin
  Form2.moLog.Lines.Insert(0,'[' + DateTimeToStr(Now) + ']: ' + ALogEntry);
end;

procedure TForm2.FormCreate(Sender: TObject);
var
  sUserCacheFolder,sExeFolder: UnicodeString;
begin
  sExeFolder := ExtractFileDir(Application.ExeName);
  sUserCacheFolder := sExeFolder + '\edgecache';
  if not ForceDirectories(sUserCacheFolder) then
    raise Exception.Create('Error: Failed to create Edge Browser user cache folder. Check directory file access rights.');

  FEB := TEdgeBrowser.Create(tsEB);
  FEB.Parent := tsEB;
  FEB.Align := alClient;
  if not FEB.WebViewCreated then
    FEB.CreateWebView;
  FEB.OnCreateWebViewCompleted := CreateWebViewCompleted;
  FEB.OnNavigationStarting := NavigationStarting;
  FEB.OnNavigationCompleted := NavigationCompleted;
  FEB.UserDataFolder := sUserCacheFolder;
  FEB.BrowserExecutableFolder := sExeFolder;
end;

procedure TForm2.NavigationCompleted(Sender: TCustomEdgeBrowser;     IsSuccess: Boolean; WebErrorStatus: COREWEBVIEW2_WEB_ERROR_STATUS);
var
  sErr: UnicodeString;
  yr,mon,day,hr,min,sec,msec: Word;
begin
  if IsSuccess then
    LogEntry('Navigation completed successfully');
  sErr := '';
  case WebErrorStatus of
//    COREWEBVIEW2_WEB_ERROR_STATUS_UNKNOWN: sErr := 'UNKNOWN'; // seems to be useless
    COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_COMMON_NAME_IS_INCORRECT: sErr := 'CERTIFICATE_COMMON_NAME_IS_INCORRECT';
    COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_EXPIRED: sErr := 'CERTIFICATE_EXPIRED';
    COREWEBVIEW2_WEB_ERROR_STATUS_CLIENT_CERTIFICATE_CONTAINS_ERRORS: sErr := 'CLIENT_CERTIFICATE_CONTAINS_ERRORS';
    COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_REVOKED: sErr := 'CERTIFICATE_REVOKED';
    COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_IS_INVALID: sErr := 'CERTIFICATE_IS_INVALID';
    COREWEBVIEW2_WEB_ERROR_STATUS_SERVER_UNREACHABLE: sErr := 'SERVER_UNREACHABLE';
    COREWEBVIEW2_WEB_ERROR_STATUS_TIMEOUT: sErr := 'TIMEOUT';
    COREWEBVIEW2_WEB_ERROR_STATUS_ERROR_HTTP_INVALID_SERVER_RESPONSE:         sErr := 'ERROR_HTTP_INVALID_SERVER_RESPONSE';
    COREWEBVIEW2_WEB_ERROR_STATUS_CONNECTION_ABORTED: sErr := 'CONNECTION_ABORTED';
    COREWEBVIEW2_WEB_ERROR_STATUS_CONNECTION_RESET: sErr := 'CONNECTION_RESET';
    COREWEBVIEW2_WEB_ERROR_STATUS_DISCONNECTED: sErr := 'DISCONNECTED';
    COREWEBVIEW2_WEB_ERROR_STATUS_CANNOT_CONNECT: sErr := 'CANNOT_CONNECT';
    COREWEBVIEW2_WEB_ERROR_STATUS_HOST_NAME_NOT_RESOLVED: sErr := 'HOST_NAME_NOT_RESOLVED';
    COREWEBVIEW2_WEB_ERROR_STATUS_OPERATION_CANCELED: sErr := 'OPERATION_CANCELED';
    COREWEBVIEW2_WEB_ERROR_STATUS_REDIRECT_FAILED: sErr := 'REDIRECT_FAILED';
    COREWEBVIEW2_WEB_ERROR_STATUS_UNEXPECTED_ERROR: sErr := 'UNEXPECTED_ERROR';
  end;
  if Length(sErr) > 0 then
    LogEntry('Navigation completed error: ' + sErr);
  FStop := Now;
  DecodeDateTime(FStop - FStart,yr,mon,day,hr,min,sec,msec);
  edNavElapsedTime.Text := IntToStr(sec);
end;

procedure TForm2.NavigationStarting(Sender: TCustomEdgeBrowser; Args: TNavigationStartingEventArgs);
var
  pwc: PWideChar;
begin
  FStart := Now;
  Args.ArgsInterface.Get_uri(pwc);
  LogEntry('Navigation Starting to ' + pwc);
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  FEB.Navigate(edit1.text);
end;

procedure TForm2.CreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HResult);
begin
  if AResult = 0 then
    LogEntry('Create Web View Completed Successfully')
  else
    LogEntry('Error: Failed to create Web View ' + IntToStr(AResult));
end;

end.

You can find the latest webview2 runtime here: NuGet Gallery | Microsoft.Web.Webview2 1.0.1072.54

Also you will need to ensure that Microsoft Edge is installed, preferably the latest version.

DeCoder
  • 173
  • 1
  • 15
  • What happens if you build a very simple program loading a single page? Build that as a complete project you'll publish here and we'll then be able to reproduce your problem...or not. – fpiette Jan 31 '22 at 16:27
  • Why have you commented that error out of the code you show? What happens if you enable it back? – Poul Bak Jan 31 '22 at 19:46
  • If you are referring to COREWEBVIEW2_WEB_ERROR_STATUS_UNKNOWN, I commented it out because every time the TEdgeBrowser successfully navigates to a URL this is the error that comes back. In other words, it appears to be a default error code response to all navigation requests. – DeCoder Jan 31 '22 at 20:13
  • One thing that I just discovered is that it appears that once I perform instantiation of the control, if I just wait for the requisite 20 seconds or so, it will navigate swiftly. Not sure where to pursue this from here, other than some sort of pre-load scheme prior to first use... – DeCoder Jan 31 '22 at 20:54
  • Investigate why the Initialization of the WebView2Loader is so slow. Often automatic proxy configuration can cause this. Ex. https://github.com/MicrosoftEdge/WebView2Feedback/issues/1818 and https://textslashplain.com/2020/07/14/web-proxy-auto-discovery/ – Brian Feb 01 '22 at 13:57
  • @Brian - Thanks for the pointers! This does appear to solve the problem. My only concern with this 'solution' is that these settings are typically managed at customer sites via group policy. Hopefully we can coordinate to get this 'adjusted' at sites. THANKS! – DeCoder Feb 01 '22 at 15:17

0 Answers0