What is the difference between BrowserWindow and <Webview> Tag in electron and when is it advisable to use each?

I can understand why it would be confusing on which of these to host your content in given their similarities. They both start in separate processes and have many similar configurations. The key difference between the BrowserWindow and the webview is that the BrowserWindow is a window on the platform and a webview is an element on an webpage This may be a bit of an obvious, superficial distinction, but much of their differences and usages derive from it.

Much of the reason a webview exists is to allow for untrusted content to be embedded within your application. If you read up on the use cases for the webview, a lot of them point to the fact that the BrowserWindow, by default, has complete access to the Node APIs. Hosting untrusted content within it is handing that content significant access to your system and presents a security concern. The webview, however, does not have Node integration turned on by default, and so it shields your application, and the platform, from the hosted content.

However, this distinction is a bit of a red herring as Node integration can be disabled on BrowserWindow and can be enabled on a webview element. That is to say, you should be able to safely host untrusted content in a BrowserWindow by taking away access to Node and host trusted content in a webview and provide it access to Node.

The key to the webview is that it allows the embedding of untrusted content on a webpage/view in your application. If, within the same view/page, you would like to have some content that is trusted with full access to Node APIs and some content that is untrusted and given limited or no access to Node APIs then this may only be accomplished with the webview element. It is a way to segregate and lock down a piece of a webpage hosted in a BrowserWindow while allowing the rest of it to be open.

Aside from embedding untrusted content, the only other case I can think of for using webviews over BrowserWindows is if you want to open and view multiple separate processes in a single window. An application could choose to create 10 different windows for 10 different processes and have the platform handle layout, focus, etc or it could open 1 window with 10 webviews for 10 different processes and handle the layout, focus, etc itself within that window.

(Edit) To address the edit to the question:

For both of these cases I would suggest using a webview.

In the first scenario, a browser, you mentioned "tabs". There is no easy, cross-platform method that I know of for building a tabbed applications using multiple BrowserWindows because the windows are created by the native OS. You could, however, achieve this by creating a tabbed application within a single webpage, each tab containing a webview. In this case you would want to ensure Node integration is disabled on the webview (it should be by default), since loading content from the web is generally untrusted.

The second scenario, an editor with rendered HTML, is not as clear cut. You could use a webview, an iframe, or render the content directly to a div. Rendering directly to a div may be the best option for something like Markdown or small snippets of HTML so long as you do not need custom css or want to execute JavaScript. It otherwise makes sense to use a webview or an iframe. The difference being that a webview starts in a separate process and may have Node integration or flexed security whereas an iframe is loaded within the same process as the BrowserWindow and, I think, has locked down security. Regardless, to get a side-by-side look without another window you need to use a HTML element like a webview and not a BrowserWindow.


Electron 5 suggests using browserview/iframe instead of webview.
https://electronjs.org/docs/api/webview-tag