How can I persist sessions across electron app close?

Note: This was simultaneously posted on

I have a couple of sites that I visit regularly. In fact, so much so, that I like to have a small electron app on my machine to check for updates (since the site doesn’t support email updates or anything of that nature). My script basically just launches a browser, retrieves web content for the site, logs it to a file, and then checks for differences. It then notifies me of a difference, if there is one.

The catch, though, is that while the app is open (even if I close the individual window where the login happened), I can go to these sites and my session information is transferred just fine without me having to login. However, if I close the main app process and reopen it, then it requires me to login again (usually with some indication that my session timed out).

Surprisingly, this behavior does not happen if I navigate to the site(s) on my normal browsers (Chrome and Firefox). That is, I can close and reopen the browser as many times as I want, and I’ll still be logged in.

It seems to me to be something that isn’t being correctly persisted in the cookie database on my local machine.

(By the way, I’m not talking about closing the app and reopening it some time later, I’m talking about closing the app, and immediately reopening it, so it seems unlikely that the session would have expired on the server-side, unless the server is getting some message from the browser that the browser is being closed, which also seems unlikely).

My configuration for the BrowserWindow is here:

  const { BrowserWindow } = require('electron').remote;

  let win = new BrowserWindow({width: 800,
    height: 600,
    webPreferences: {
      partition: 'persist:sitePoller'

Also, this appears to work just fine for a site like, but not for an ASP-based site, which seems to be the thing all of the sites that I want to work with (and can’t) have in common.

1 Like

I’m using this workaround. To keep cookies for the running session we need to store cookies which are considered to be session cookies. Then restore it on next run.

If you know better solution. let me know.

  for (let cookie of storedCookies) {
    let pageUrl = 'https://' + cookie.domain.replace(/^\./, '') + '/'
    let cookiedata = {
      url: pageUrl,
      value: cookie.value,
      path: cookie.path || '/',
      httpOnly: cookie.httpOnly,
  await fs.promises.mkdir(userSessionDir, { recursive: true })
  wc.on('did-navigate', (event, url) => {
  wc.on('did-navigate-in-page', (event, url, isMainFrame) => {

Hi @hosseinamin

Can you share your fully working code?

Thank you.