Skip to content

Commit

Permalink
fix: do not wait on promise returned by remote APIs (#18990)
Browse files Browse the repository at this point in the history
* fix: make <webview>.loadURL async

* docs: webview.loadURL returns Promise
  • Loading branch information
zcbenz authored and John Kleinschmidt committed Jul 10, 2019
1 parent 015e134 commit faa2710
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/api/webview-tag.md
Expand Up @@ -277,6 +277,11 @@ webview.addEventListener('dom-ready', () => {
* `postData` ([UploadRawData[]](structures/upload-raw-data.md) | [UploadFile[]](structures/upload-file.md) | [UploadBlob[]](structures/upload-blob.md)) (optional)
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.

Returns `Promise<void>` - The promise will resolve when the page has finished loading
(see [`did-finish-load`](webview-tag.md#event-did-finish-load)), and rejects
if the page fails to load (see
[`did-fail-load`](webview-tag.md#event-did-fail-load)).

Loads the `url` in the webview, the `url` must contain the protocol prefix,
e.g. the `http://` or `file://`.

Expand Down
2 changes: 1 addition & 1 deletion lib/common/web-view-methods.js
Expand Up @@ -3,7 +3,6 @@
// Public-facing API methods.
exports.syncMethods = new Set([
'getURL',
'loadURL',
'getTitle',
'isLoading',
'isLoadingMainFrame',
Expand Down Expand Up @@ -54,6 +53,7 @@ exports.syncMethods = new Set([
])

exports.asyncMethods = new Set([
'loadURL',
'capturePage',
'executeJavaScript',
'insertCSS',
Expand Down
2 changes: 1 addition & 1 deletion lib/renderer/web-view/web-view-attributes.ts
Expand Up @@ -196,7 +196,7 @@ class SrcAttribute extends WebViewAttribute {
const method = 'loadURL'
const args = [this.getValue(), opts]

ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', guestInstanceId, method, args)
ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', guestInstanceId, method, args)
}
}

Expand Down
14 changes: 14 additions & 0 deletions spec/webview-spec.js
Expand Up @@ -170,6 +170,20 @@ describe('<webview> tag', function () {
expect(webview.src).to.equal('')
}
})

it('does not wait until loadURL is resolved', async () => {
await loadWebView(webview, { src: 'about:blank' })

const before = Date.now()
webview.src = 'https://github.com'
const now = Date.now()

// Setting src is essentially sending a sync IPC message, which should
// not exceed more than a few ms.
//
// This is for testing #18638.
expect(now - before).to.be.below(100)
})
})

describe('nodeintegration attribute', () => {
Expand Down

0 comments on commit faa2710

Please sign in to comment.