Repository: jiahaog/nativefier
File: app/src/components/mainWindow.ts (TypeScript)

Code before:
  if (options.maximize) {
    mainWindow.maximize();
    options.maximize = undefined;
    saveAppArgs(options);
  }

  if (options.tray === 'start-in-tray') {
    mainWindow.hide();
  }
  createMenu(options, mainWindow);
  createContextMenu(options, mainWindow);
  setupNativefierWindow(options, mainWindow);

  // .on('new-window', ...) is deprected in favor of setWindowOpenHandler(...)
  // We can't quite cut over to that yet for a few reasons:
  // 1. Our version of Electron does not yet support a parameter to
  //    setWindowOpenHandler that contains `disposition', which we need.
  //    See https://github.com/electron/electron/issues/28380
  // 2. setWindowOpenHandler doesn't support newGuest as well
  // Though at this point, 'new-window' bugs seem to be coming up and downstream
  // users are being pointed to use setWindowOpenHandler.
  // E.g., https://github.com/electron/electron/issues/28374

  mainWindow.webContents.on(
    'new-window',
    (event, url, frameName, disposition) => {
      onNewWindow(
        options,
        setupNativefierWindow,
        event,
        url,
        frameName,
        disposition,
      ).catch((err) => log.error('onNewWindow ERROR', err));
    },
  );

  if (options.counter) {
    setupCounter(options, mainWindow, setDockBadge);
  } else {
    setupNotificationBadge(options, mainWindow, setDockBadge);
  }

  ipcMain.on('notification-click', () => {
    log.debug('ipcMain.notification-click');
    mainWindow.show();
  });

  setupSessionInteraction(options, mainWindow);

  if (options.clearCache) {

Review comment (suggestion):
```suggestion
  // Note it is important to add this handler only to the *main* window,
  // else we run into weird behavior like opening tabs twice
  mainWindow.webContents.on(
```

Code after:
  if (options.maximize) {
    mainWindow.maximize();
    options.maximize = undefined;
    saveAppArgs(options);
  }

  if (options.tray === 'start-in-tray') {
    mainWindow.hide();
  }
  createMenu(options, mainWindow);
  createContextMenu(options, mainWindow);
  setupNativefierWindow(options, mainWindow);

  // .on('new-window', ...) is deprected in favor of setWindowOpenHandler(...)
  // We can't quite cut over to that yet for a few reasons:
  // 1. Our version of Electron does not yet support a parameter to
  //    setWindowOpenHandler that contains `disposition', which we need.
  //    See https://github.com/electron/electron/issues/28380
  // 2. setWindowOpenHandler doesn't support newGuest as well
  // Though at this point, 'new-window' bugs seem to be coming up and downstream
  // users are being pointed to use setWindowOpenHandler.
  // E.g., https://github.com/electron/electron/issues/28374

  // Note it is important to add this handler only to the *main* window,
  // else we run into weird behavior like opening tabs twice
  mainWindow.webContents.on(
    'new-window',
    (event, url, frameName, disposition) => {
      onNewWindow(
        options,
        setupNativefierWindow,
        event,
        url,
        frameName,
        disposition,
      ).catch((err) => log.error('onNewWindow ERROR', err));
    },
  );

  if (options.counter) {
    setupCounter(options, mainWindow, setDockBadge);
  } else {
    setupNotificationBadge(options, mainWindow, setDockBadge);
  }

  ipcMain.on('notification-click', () => {
    log.debug('ipcMain.notification-click');
    mainWindow.show();
  });

  setupSessionInteraction(options, mainWindow);