Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadURL / loadFile calls throw "ERR_ABORTED (-3) error" if location.hash or history.pushState called on the page before page loaded #17526

Closed
vladimiry opened this issue Mar 24, 2019 · 9 comments · Fixed by #18109

Comments

@vladimiry
Copy link

vladimiry commented Mar 24, 2019

Electron v5.0.0-beta.6 will likely break every single page application / SPA that uses routing as it's uncommon to trigger the routing only after the page loading event has been fired. See workaround in additional Information section below.

Issue Details

  • Electron Version:
    • 5.0.0-beta.6 +
  • Operating System:
    • any (tested on linux)
  • Last Known Working Electron version:
    • 4.1.1

Expected Behavior

Page loading completes without errors even if that's a SPA.

Actual Behavior

  • window.location.hash = '123' called on page:
{ Error: ERR_ABORTED (-3) loading 'file:////dev/temp/electron-loadurl-issue/page.html#123'
    at rejectAndCleanup (//dev/temp/electron-loadurl-issue/node_modules/electron/dist/resources/electron.asar/browser/navigation-controller.js:72:21)
    at WebContents.navigationListener (//dev/temp/electron-loadurl-issue/node_modules/electron/dist/resources/electron.asar/browser/navigation-controller.js:93:20)
    at WebContents.emit (events.js:193:15)
  errno: -3,
  code: 'ERR_ABORTED',
  url:
   'file:////dev/temp/electron-loadurl-issue/page.html#123' }
  • history.pushState({}, "title", "url") called on page:
{ Error: ERR_ABORTED (-3) loading 'file:////dev/temp/electron-loadurl-issue/url'
    at rejectAndCleanup (//dev/temp/electron-loadurl-issue/node_modules/electron/dist/resources/electron.asar/browser/navigation-controller.js:72:21)
    at WebContents.navigationListener (//dev/temp/electron-loadurl-issue/node_modules/electron/dist/resources/electron.asar/browser/navigation-controller.js:93:20)
    at WebContents.emit (events.js:193:15)
  errno: -3,
  code: 'ERR_ABORTED',
  url: 'file:////dev/temp/electron-loadurl-issue/url' }

To Reproduce

  • main.js:
const path = require("path");
const url = require("url");
const {app, BrowserWindow} = require("electron");

app.on("ready", async () => {
    const browserWindow = new BrowserWindow();

    browserWindow.webContents.on("did-start-navigation", (event, url) => {
        console.log("did-start-navigation", {url});
    });

    try {
        await browserWindow.loadURL(
            url.format({
                protocol: "file",
                slashes: true,
                pathname: path.join(__dirname, "page.html")
            })
        );
    } catch (e) {
        console.error(e);
    }

    // "loadFile" gives the same result
    // try {
    //     await browserWindow.loadFile(
    //         path.join(__dirname, "page.html"),
    //     );
    // } catch (e) {
    //     console.error(e);
    // }
});
  • page.html:
<!DOCTYPE html>
<html>
<body>
<script>
    window.location.hash = '123';
    // history.pushState({}, "title", "url"); // second case
</script>
</body>
</html>

Screenshots

N/A

Additional Information

The workaround is changing the hash / history state afte page got loaded:

<!DOCTYPE html>
<html>
<script>
    function onload() {
        window.location.hash = '123';
        // history.pushState({}, "title", "url"); // second case
    }
</script>
<body onload="onload">
</body>
</html>

See relater PR #15855 and code line https://github.com/electron/electron/pull/15855/files#diff-fc5b6704bed639bfab5927de1eccbcc3R85

CC @nornagon

@vladimiry vladimiry changed the title loadURL / loadFile calls throw "ERR_ABORTED (-3) error" if window.location.hash or history.pushState called on the page loadURL / loadFile calls throw "ERR_ABORTED (-3) error" if window.location.hash or history.pushState called on the page befor page loaded Mar 25, 2019
@vladimiry vladimiry changed the title loadURL / loadFile calls throw "ERR_ABORTED (-3) error" if window.location.hash or history.pushState called on the page befor page loaded loadURL / loadFile calls throw "ERR_ABORTED (-3) error" if location.hash or history.pushState called on the page before page loaded Mar 25, 2019
@sofianguy sofianguy added this to Unsorted Issues in 5.0.x Mar 25, 2019
@sofianguy sofianguy moved this from Unsorted Issues to Doesn't Block Stable in 5.0.x Mar 28, 2019
@MarshallOfSound
Copy link
Member

Hey @vladimiry

This is currently the expected behavior, the promise that loadURL returns will be rejected if the renderer for whatever reason fails to completely load that URL.

If you navigate before the page has completed loading you have aborted that load, hence the ERR_ABORTED rejection.

@vladimiry
Copy link
Author

vladimiry commented Mar 29, 2019

@MarshallOfSound I still believe such logic is going to break a lot of SPA apps, but I got it that it's expected behavior. There are a few points I'd like you to consider:

  • Changing the hash or push state is not really navigation, the new page loading process is not involved but only the url gets changed, ie changing the hash or push state is completely happening on the client side (browser side in term of server-client scenario).
  • The currently shown error is vague, I had to look into the sources in order to understand why exactly page loading got aborted.

@MarshallOfSound
Copy link
Member

@vladimirkononov

I still believe such logic is going to break a lot of SPA apps

How can it break apps? This feature was only just introduced in 5.0.0, it didn't exist in any other state before that version.

@vladimiry
Copy link
Author

vladimiry commented Mar 29, 2019

@MarshallOfSound Single page applications often use routing based on either html5 history api (push state thing) or url hash. Normally SPA doesn't wait for the full page loading but starts working asap which means html5 hitstory or url hash might be changed before page got fully loaded and it doesn't mean that a new page starts loading but it's only the client side url change. This is actually how I noticed the problem in action. See applied workaround here
https://github.com/vladimiry/ElectronMail/blob/0fb48fa16fe4dcf0370a9004c4556f0716941377/src/web/src/environments/production/bootstrap-app.ts#L10 as an example.

This feature was only just introduced in 5.0.0

Correct. I started trying v5 beta in action and I open issues if I face bugs or blockers (this one, for example, got successfully fixed). I think it's better to handle the apparent issue before final v5 release gets published.

I think at least making the error message more detailed would be a good idea.

@cyphercodes
Copy link

This is also blocking loading dataURL like so:

browserWindow.webContents.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(html), {
      baseURLForDataURL: httpHost,
});

@vladimiry
Copy link
Author

If you navigate before the page has completed loading you have aborted that load, hence the ERR_ABORTED rejection.

I will just repeat. Calling history.pushState or changing the location.hash value does trigger reloading the page, so the loaded page remains the same. This is why expected behavior does not look good for me.

@cyphercodes
Copy link

This is also blocking loading dataURL like so:

browserWindow.webContents.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(html), {
      baseURLForDataURL: httpHost,
});

This is still happening even after 5.0.2

vladimiry added a commit to vladimiry/ElectronMail that referenced this issue Jun 16, 2019
vladimiry added a commit to vladimiry/ElectronMail that referenced this issue Jun 17, 2019
@preslavsh
Copy link

Still happening in 6.0.9. I not sure what the proper behavior should be, but it causes pain when I want to load angular hash route from electron. My case is notification window should open hash route on the main window.

@vladimiry
Copy link
Author

vladimiry commented Sep 23, 2019

@preslavsh you could try bootstrapping the Angular app in window.addEventListener("load" handler, ugly workaround though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
5.0.x
Doesn't Block Stable
Development

Successfully merging a pull request may close this issue.

5 participants