Setting webview.src to unreachable url doesn't emit 'did-fail-load' event


#1

When I’m trying to load a url in webview, it doesn’t fire ‘did-fail-load’ when that url is unreachable or the webserver itself is down. however, later, when i reload the webview, it does fire ‘did-fail-load’ after some timeout.

Is it a known issue that it doesn’t emit ‘did-fail-load’ event for the first time, but does it when called webview.reload() ? Or should i be having listener for the some other type of event?


#2

I see the did-fail-load event immediately after setting the src to an unreachable URL. Here is my test code:

<webview id="x"></webview>
<script>
  wv = document.getElementById('x')
  document.addEventListener('DOMContentLoaded', function () {
    wv.src = 'http://domain.tld/'
  })
  wv.addEventListener('did-fail-load', function (ev) {
    console.log('did-fail-load', ev)
  })
</script>

#3

Thanks @john. That helps…

However, with the following code, i noticed that ‘dom-ready’ and ‘did-finish-load’ events were fired twice.

main.js

app.on('ready', function() {
    mainWindow = new BrowserWindow({
        width: 480,
        height: 640,
        resizable: false
    }); 
    mainWindow.loadUrl('file://' + __dirname + '/main.html');

    mainWindow.on('closed', function() {
        mainWindow = null;
    }); 

    ipc.on('child:open', function(event) {
        console.log('main.js :: ipc msg child:open received');
        openChildWindow();
    }); 

    openChildWindow = function() {
        if (!!childWindow) {
            console.log('main.js :: child window already open, sending msg to it');
            childWindow.webContents.send('url', "http://www.yahoo.com");
        } else {
            console.log('main.js :: create a new child window');
            childWindow = new BrowserWindow({
                width: 1080,
                height: 640,
            }); 
            childWindow.loadUrl('file://' + __dirname + '/child.html');

            childWindow.webContents.on('dom-ready', function() {
                console.log('main.js :: new child window loaded. sending url to the child window.');
                childWindow.webContents.send('url', "http://domain.tld");
                childWindow.show();
            }); 

            childWindow.on('closed', function() {
                console.log('main.js :: child window closed');
                childWindow = null;
            }); 
        }
    };
});

main.html

<html>  
<head>  
</head> 
<body>
    main window
    <button onclick="openChild()"> open child </button>
    <script>
        var ipc = require('ipc');
        function openChild() {
            console.log('open child button clicked. sending child:open ipc msg to main process');
            ipc.sendSync('child:open');
        }
    </script>
</body>     
</html>

child.html

<html>
<head>
</head>
<body>
    child window
    <webview src="about:blank" id="content" autosize="on"></webview>
    <script>
        var ipc = require('ipc');
        var webview = document.getElementById('content');
        
        ipc.on('url', function(url) {
            console.log('child :: url received: ' + url);
            webview.src = url;
        });
        
        webview.addEventListener('did-fail-load', function(e) {
            console.log('child :: failed to load - ' + webview.src);
            console.log('e : ' + JSON.stringify(e));
        });

        webview.addEventListener('did-finish-load', function() {
            console.log('child :: finished loading - ' + webview.src);
        });

        webview.addEventListener('dom-ready', function() {
            console.log('child :: dom-ready fired - ' + webview.src);
        });

        webview.addEventListener('did-get-response-details', function() {
            console.log('child :: got response - ' + webview.src);
        });
    </script>
</body>
</html>

Console log output

[80101:0830/180437:INFO:CONSOLE(11)] "open child button clicked. sending child:open ipc msg to main process", source: file:///Users/sreekanth/Downloads/_personal_work/sandbox/sample-electron-connect/main.html (11)
main.js :: ipc msg child:open received
main.js :: create a new child window
main.js :: new child window loaded. sending url to the child window.
[80101:0830/180437:INFO:CONSOLE(15)] "child :: url received: http://domain.tld", source: file:///Users/sreekanth/Downloads/_personal_work/sandbox/sample-electron-connect/child.html (15)
[80101:0830/180438:INFO:CONSOLE(29)] "child :: dom-ready fired - http://domain.tld/", source: file:///Users/sreekanth/Downloads/_personal_work/sandbox/sample-electron-connect/child.html (29)
[80101:0830/180438:INFO:CONSOLE(25)] "child :: finished loading - http://domain.tld/", source: file:///Users/sreekanth/Downloads/_personal_work/sandbox/sample-electron-connect/child.html (25)
[80101:0830/180438:INFO:CONSOLE(20)] "child :: failed to load - http://domain.tld/", source: file:///Users/sreekanth/Downloads/_personal_work/sandbox/sample-electron-connect/child.html (20)
[80101:0830/180438:INFO:CONSOLE(21)] "e : {"errorCode":-105,"errorDescription":""}", source: file:///Users/sreekanth/Downloads/_personal_work/sandbox/sample-electron-connect/child.html (21)
[80101:0830/180438:INFO:CONSOLE(29)] "child :: dom-ready fired - http://domain.tld/", source: file:///Users/sreekanth/Downloads/_personal_work/sandbox/sample-electron-connect/child.html (29)
[80101:0830/180438:INFO:CONSOLE(25)] "child :: finished loading - http://domain.tld/", source: file:///Users/sreekanth/Downloads/_personal_work/sandbox/sample-electron-connect/child.html (25)

The last 2 lines of the console logs indicate that the dom-ready and did-finish-load events got fired twice, for the unreachable url… Is this the expected behavior? Or is it a bug?