Issue 1:
Electron version: Tested on 1.6.2 and 1.7.x

Operating system: MacOS

The webview canGoBack/canGoForward, and actual goBack/goForward actions, seem to get confused with Twitter.

You can observe this by visiting twitter from a webview:

Demo: https://github.com/pfrazee/electron-bug-twitter-history

$ git clone https://github.com/pfrazee/electron-bug-twitter-history
$ cd electron-bug-twitter-history
$ npm install
$ npm start

Issue 2:
Electron Version: 2.0.2

Operating System (Platform and Version): macOS 10.13.2

Last known working Electron version: N/A

Expected Behavior

WebView history remains correct after navigating with goBack()

Actual behavior

Some webpages, such as https://twitter.com/, load a navigation upon goBack() being called, (in this case it is a navigation to https://twitter.com/), and this navigation triggers this.webContents.on('navigation-entry-commited') before the navigation from goBack() does, resulting in the wrong blocks code being executed and the WebView history to be messed up.

Here's the relevant code in navigation-controller.js:

this.webContents.on('navigation-entry-commited', (event, url, inPage, replaceEntry) => {
if (this.inPageIndex > -1 && !inPage) {
// Navigated to a new page, clear in-page mark.
this.inPageIndex = -1
} else if (this.inPageIndex === -1 && inPage && !replaceEntry) {
// Started in-page navigations.
this.inPageIndex = this.currentIndex
}
if (this.pendingIndex >= 0) {
// Go to index.
this.currentIndex = this.pendingIndex
this.pendingIndex = -1
this.history[this.currentIndex] = url
} else if (replaceEntry) {
// Non-user initialized navigation.
this.history[this.currentIndex] = url
} else {
// Normal navigation. Clear history.
this.history = this.history.slice(0, this.currentIndex + 1)
this.currentIndex++
this.history.push(url)
}
})

NavigationController.prototype.goBack = function () {
if (!this.canGoBack()) {
return
}
this.pendingIndex = this.getActiveIndex() - 1
if (this.inPageIndex > -1 && this.pendingIndex >= this.inPageIndex) {
return this.webContents._goBack()
} else {
return this.webContents._loadURL(this.history[this.pendingIndex], {})
}
}

And here's a log of what happens, with some edits to point out where it all goes wrong:

this.history: [ 'about:blank', 'https://www.google.com/?gws_rd=ssl', 'https://twitter.com/' ]
goBack() called:
this.pendingIndex changed to 1
loadURL(https://www.google.com/?gws_rd=ssl) called
this.webContents.on('navigation-entry-commited' event triggered: <--- this should be from calling loadURL(https://www.google.com/?gws_rd=ssl), but it's not!
"// Go to index." block executed:
this.currentIndex changed to 1
this.pendingIndex changed to -1
this.history[1] changed to: https://twitter.com/ <--- !!!!!!! url is wrong! Expected to be https://www.google.com/?gws_rd=ssl
this.history: [ 'about:blank', 'https://twitter.com/', 'https://twitter.com/' ] <--- !!!!!!! Now this.history is messed up
this.webContents.on('navigation-entry-commited' event triggered: <--- now this is from calling loadURL(https://www.google.com/?gws_rd=ssl), but it's after the previous weird navigation event
"// Normal navigation. Clear history." block executed: <--- because of the previous event, this.pendingIndex was changed back to -1, so the wrong block of code is executed
currentIndex is 1
this.history sliced from 0 to (currentIndex + 1)
this.history: [ 'about:blank', 'https://twitter.com/' ]
currentIndex changed to 2
"https://www.google.com/?gws_rd=ssl" pushed to history
this.history: [ 'about:blank', 'https://twitter.com/', 'https://www.google.com/?gws_rd=ssl' ] <--- now this.history is really messed up

To Reproduce

see #9835

Screenshots

see #9835