Skip to content

docs: type WebContentsView.webContents as possibly undefined - #56068

Closed
panther7 wants to merge 1 commit into
electron:mainfrom
panther7:docs/web-contents-view-nullable-web-contents
Closed

panther7 wants to merge 1 commit into
electron:mainfrom
panther7:docs/web-contents-view-nullable-web-contents

Conversation

@panther7

Copy link
Copy Markdown
Contributor

Description of Change

view.webContents is documented and typed as WebContents, but it is undefined once the underlying WebContents has been destroyed. Code that trusts the typings and dereferences it throws:

TypeError: Cannot read properties of undefined (reading 'isDestroyed')

Observed in a production app, in a listener registered with browserWindow.once('closed', ...):

const view = new WebContentsView()
win.contentView.addChildView(view)

win.once('closed', () => {
  // throws if view.webContents was destroyed before the window closed
  if (!view.webContents.isDestroyed()) view.webContents.close()
})

The WebContents of a WebContentsView is not destroyed together with the window, so apps close it explicitly. If it was destroyed earlier (closed elsewhere, failed load cleanup, renderer teardown), the later closed handler reads view.webContents and gets undefined.

Cause. In shell/browser/api/electron_api_web_contents_view.cc, WebContentsDestroyed() clears api_web_contents_, and the webContents getter (evaluated on every read) then returns an empty handle from GetWebContents(). JavaScript sees undefined (this is what the crash report shows; I did not trace the V8 conversion of the empty handle itself). docs/api/web-contents-view.md says only "A WebContents property", so the generated electron.d.ts has readonly webContents: WebContents.

Why it matters. The typings are wrong for a reachable state, so the compiler cannot warn about the unsafe access, and linters that trust the types (for example @typescript-eslint/no-unnecessary-condition) flag the correct defensive check if (view.webContents && ...) as unnecessary. Users are pushed towards the crashing form. Similar mismatches were fixed in docs before: #53248 (nullable webContents in app login and select-client-certificate).

Change. docs/api/web-contents-view.md describes the property as WebContents | undefined, with a sentence that it is undefined after the WebContents is destroyed. Regenerating electron.d.ts with npm run create-typescript-definitions gives:

readonly webContents: (WebContents) | (undefined);

Not run: lint:markdown, lint:docs-relative-links, lint:api-history.

Impact. Docs and generated typings only, no runtime change. TypeScript users reading view.webContents now need ?., ! or a guard. The example in the docs (view.webContents.loadURL(...) right after new WebContentsView()) still works at runtime but would not compile without one of those.

Open point. The docs say undefined because the getter returns an empty handle and the observed value is undefined; other properties such as contents.devToolsWebContents use null. An alternative is to make the getter return null explicitly, but that is a behaviour change and out of scope here.

Checklist

Release Notes

Notes: Fixed WebContentsView.webContents typings to include undefined.

The getter returns an empty handle after the WebContents is destroyed (WebContentsView::WebContentsDestroyed clears api_web_contents_), so JS sees undefined. Document it so electron.d.ts matches.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation 📓 semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant