Repository navigation
Conversation
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.
1 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Change
view.webContentsis documented and typed asWebContents, but it isundefinedonce the underlyingWebContentshas been destroyed. Code that trusts the typings and dereferences it throws:Observed in a production app, in a listener registered with
browserWindow.once('closed', ...):The
WebContentsof aWebContentsViewis not destroyed together with the window, so apps close it explicitly. If it was destroyed earlier (closed elsewhere, failed load cleanup, renderer teardown), the laterclosedhandler readsview.webContentsand getsundefined.Cause. In
shell/browser/api/electron_api_web_contents_view.cc,WebContentsDestroyed()clearsapi_web_contents_, and thewebContentsgetter (evaluated on every read) then returns an empty handle fromGetWebContents(). JavaScript seesundefined(this is what the crash report shows; I did not trace the V8 conversion of the empty handle itself).docs/api/web-contents-view.mdsays only "AWebContentsproperty", so the generatedelectron.d.tshasreadonly 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 checkif (view.webContents && ...)as unnecessary. Users are pushed towards the crashing form. Similar mismatches were fixed in docs before: #53248 (nullablewebContentsinapploginandselect-client-certificate).Change.
docs/api/web-contents-view.mddescribes the property asWebContents | undefined, with a sentence that it isundefinedafter theWebContentsis destroyed. Regeneratingelectron.d.tswithnpm run create-typescript-definitionsgives:Not run:
lint:markdown,lint:docs-relative-links,lint:api-history.Impact. Docs and generated typings only, no runtime change. TypeScript users reading
view.webContentsnow need?.,!or a guard. The example in the docs (view.webContents.loadURL(...)right afternew WebContentsView()) still works at runtime but would not compile without one of those.Open point. The docs say
undefinedbecause the getter returns an empty handle and the observed value isundefined; other properties such ascontents.devToolsWebContentsusenull. An alternative is to make the getter returnnullexplicitly, but that is a behaviour change and out of scope here.Checklist
Release Notes
Notes: Fixed
WebContentsView.webContentstypings to includeundefined.