Repository navigation
Description
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for a bug report that matches the one I want to file, without success.
Electron Version
44.11.5 (observed in a production app; the same getter is in 43-x-y, 44-x-y and 45-x-y, main differs, see below)
What operating system(s) are you using?
Windows
Operating System Version
Windows 11 (observed). The code involved is platform-independent.
What arch are you using?
x64
Last Known Working Electron version
No response
Does the issue also appear in Chromium / Google Chrome?
No
Expected Behavior
view.webContents is documented and typed as WebContents, so reading it should be safe at any time. Like win.webContents, it should keep returning the (destroyed) object, so that view.webContents.isDestroyed() works.
Actual Behavior
After the view's WebContents is destroyed, view.webContents no longer returns a WebContents. In a production app this throws in a closed handler:
TypeError: Cannot read properties of undefined (reading 'isDestroyed')
const view = new WebContentsView()
win.contentView.addChildView(view)
win.once('closed', () => {
if (!view.webContents.isDestroyed()) view.webContents.close()
})The WebContents of a WebContentsView is not destroyed with the window, so apps close it explicitly. If it was destroyed earlier (closed elsewhere, failed load cleanup, renderer teardown), the closed handler reads view.webContents and gets undefined.
Because electron.d.ts has readonly webContents: WebContents, the compiler does not warn, and linters that trust the types (for example @typescript-eslint/no-unnecessary-condition) flag the correct defensive check if (view.webContents && ...) as unnecessary.
Testcase Gist URL
No response
Additional Information
I have not made a standalone Fiddle; the undefined was observed in production, the rest is from reading the code.
In-depth investigation
Behaviour differs by branch (from reading the code, not run):
| Branch | Getter in shell/browser/api/electron_api_web_contents_view.cc |
Destroyed view.webContents |
|---|---|---|
| 43-x-y, 44-x-y, 45-x-y | returns gin_helper::Handle<WebContents>; WebContentsDestroyed() clears api_web_contents_, so the handle is empty |
undefined (matches the production crash) |
main, 46-x-y |
WebContents* GetWebContents() returning api_web_contents_.Get() (cppgc::WeakPersistent, cleared in OnWebContentsDestroyed()); gin converts nullptr to v8::Null |
null |
The change on main came with "refactor: webContents managed by cppgc" (#53757).
Precedent for a non-nullable getter: BrowserWindow keeps the wrapper in a v8::Global (web_contents_) and GetWebContents() returns it for the window's whole life. Only the weak api_web_contents_ is cleared on destruction, so win.webContents stays an object with isDestroyed() === true.
Possible directions:
- Make
WebContentsViewkeep the wrapper likeBrowserWindowdoes. Types and docs stayWebContents, andview.webContents.isDestroyed()keeps working. This is a behaviour change, and the cppgc ownership (theWeakPersistent) would need to be checked. - Type it as nullable. Docs and typings only, but every consumer needs a guard, and the value differs between branches (
undefinedvsnull). - Return
nullconsistently and type it asWebContents | null. Same cost to consumers as 2, plus a behaviour change on 43–45.
There is a docs-only PR, #56068, that types it as WebContents | undefined. That is only accurate for 43–45, not for main (which looks like null).
Activity