You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Normally you would just use win.loadURL('file://…'), but that doesn't work when you're making a single-page web app, which most Electron apps are today, as history.pushState()'ed URLs don't exist on disk. It serves files if they exist, and falls back to index.html if not, which means you can use router modules like react-router, vue-router, etc.
Install
npm install electron-serve
Requires Electron 30 or later.
Usage
import{app,BrowserWindow}from'electron';importservefrom'electron-serve';constloadURL=serve({directory: 'renderer'});letmainWindow;(async()=>{awaitapp.whenReady();mainWindow=newBrowserWindow();awaitloadURL(mainWindow);// Or optionally with search parameters.awaitloadURL(mainWindow,{id: 4,foo: 'bar'});// The above is equivalent to this:awaitmainWindow.loadURL('app://-');// The `-` is just the required hostname})();
API
loadUrl = serve(options)
options
Type: object
directory
Required
Type: string
The directory to serve, relative to the app root directory.
scheme
Type: string
Default: 'app'
Custom scheme. For example, foo results in your directory being available at foo://-.
hostname
Type: string
Default: '-'
Custom hostname.
file
Type: string
Default: 'index'
Custom HTML filename. This gets appended with '.html'.
isCorsEnabled
Type: boolean
Default: true
Whether CORS should be enabled.
Useful for testing purposes.