Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt dev mode do not stop on app quit #54

Closed
Ax3l3rator opened this issue Oct 30, 2023 · 8 comments
Closed

Nuxt dev mode do not stop on app quit #54

Ax3l3rator opened this issue Oct 30, 2023 · 8 comments

Comments

@Ax3l3rator
Copy link

Ax3l3rator commented Oct 30, 2023

When i press 'x' in window nuxt dev mode just stays in console
app code

import { app } from 'electron';
import { restoreOrCreateWindow } from './mainWindow';
import { platform } from 'node:process';

const isSingleInstance = app.requestSingleInstanceLock();
if (!isSingleInstance) {
  app.quit();
  process.exit(0);
}
app.on('second-instance', restoreOrCreateWindow);

app.disableHardwareAcceleration();

app.on('window-all-closed', () => {
  if (platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', restoreOrCreateWindow);

app
  .whenReady()
  .then(restoreOrCreateWindow)
  .catch((e) => console.error('Failed create window:', e));

window creation code

import { app, BrowserWindow } from 'electron';
import { join, resolve } from 'node:path';

async function createWindow() {
  const browserWindow = new BrowserWindow({
    show: false,
    webPreferences: {
      // nodeIntegration: false,
      // contextIsolation: true,
      // sandbox: false,
      // webviewTag: false,
      // preload: join(app.getAppPath(), 'preload.{ts,js}'),
    },
  });

  browserWindow.on('ready-to-show', () => {
    browserWindow?.show();
    if (import.meta.env.DEV) {
      browserWindow.webContents.on('before-input-event', (_, input) => {
        if (input.type === 'keyDown' && input.key === 'F12') {
          browserWindow.webContents.openDevTools({ mode: 'detach' });
        }
      });
    }
  });

  if (
    import.meta.env.DEV &&
    import.meta.env.VITE_DEV_SERVER_URL !== undefined
  ) {
    await browserWindow.loadURL(import.meta.env.VITE_DEV_SERVER_URL);
  } else {
    await browserWindow.loadFile(
      resolve(__dirname, '../.output/public/index.html'),
    );
  }
  browserWindow.removeMenu();
  browserWindow.setMenu(null);

  return browserWindow;
}

export async function restoreOrCreateWindow() {
  let window = BrowserWindow.getAllWindows().find((w) => !w.isDestroyed());
  if (window === undefined) {
    window = await createWindow();
  }

  if (window.isMinimized()) {
    window.restore();
  }

  window.focus();
}

EDIT: Did some reseach dsown in comments, trying to figure out what exactly causes the issue
UPD 2: crutch-fix, but works fine

@Ax3l3rator
Copy link
Author

UPD: package.json

{
  "name": "hse-app-desktop",
  "private": true,
  "scripts": {
    "build": "nuxt build --prerender && electron-builder",
    "dev": "nuxi dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@nuxt/devtools": "latest",
    "electron": "^27.0.2",
    "electron-builder": "^24.6.4",
    "nuxt": "^3.8.0",
    "nuxt-electron": "^0.6.0",
    "sass": "^1.69.5",
    "vite-plugin-electron": "^0.14.1",
    "vite-plugin-electron-renderer": "^0.14.5",
    "vite-plugin-vuetify": "^1.0.2",
    "vue": "^3.3.7",
    "vue-router": "^4.2.5"
  },
  "main": "dist-electron/index.js",
  "version": "0.0.1",
  "dependencies": {
    "@mdi/font": "^7.3.67",
    "vuetify": "^3.3.23"
  }
}

@Ax3l3rator
Copy link
Author

Ax3l3rator commented Oct 30, 2023

Note: quickstart project with following packagfe versions also do not stop nuxt dev server with electron app.quit()/app.exit()

{
  "name": "nuxt-electron-quick-start",
  "version": "0.0.0",
  "private": true,
  "author": "草鞋没号 <[email protected]>",
  "license": "MIT",
  "main": "dist-electron/main.js",
  "scripts": {
    "dev": "nuxi dev",
    "build": "nuxi build --prerender && electron-builder"
  },
  "dependencies": {},
  "devDependencies": {
    "electron": "^27.0.2",
    "electron-builder": "^24.6.4",
    "nuxt": "^3.6.3",
    "nuxt-electron": "^0.6.0",
    "vite-plugin-electron": "^0.14.1",
    "vite-plugin-electron-renderer": "^0.14.5"
  }
}

Old project with following versions works good

{
  "name": "HSEAppXWin",
  "private": true,
  "scripts": {
    "build": "nuxt build --prerender && electron-builder",
    "dev": "set DEV_URL=http://localhost:3001/&&set scrtk=hseDummyKeyLol&&nuxt dev",
    "generate": "nuxt generate && electron-builder",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@mdi/font": "^7.2.96",
    "@types/electron": "^1.6.10",
    "@types/jsonwebtoken": "^9.0.3",
    "@types/node": "^18",
    "electron": "^26.2.2",
    "electron-builder": "^24.6.4",
    "nuxt": "^3.4.3",
    "vite-electron-plugin": "^0.8.2",
    "vite-plugin-electron-renderer": "^0.14.1",
    "vite-plugin-vuetify": "^1.0.2"
  },
  "dependencies": {
    "@vee-validate/nuxt": "^4.9.0",
    "@vueuse/nuxt": "^10.1.2",
    "electron-store": "^8.1.0",
    "jsonwebtoken": "^9.0.2",
    "keytar": "^7.9.0",
    "nuxt-electron": "^0.4.5",
    "sass": "^1.62.1",
    "vuetify": "^3.2.2"
  },
  "main": "dist-electron/electron/main.js",
  "version": "1.1.2",
  "build": {
    "icon": "./build/icon.png"
  }
}

@dvwzj
Copy link

dvwzj commented Nov 4, 2023

app.on('window-all-closed', () => {
  if (platform !== 'darwin') {
    app.quit();
  }
  process.kill(process.pid, 'SIGTERM'); // add this
})

this work for me

@Ax3l3rator
Copy link
Author

Hey @dvwzj, huge thanks for your reply! Do u use process.kill in production mode too, or only for dev mode?

@yejimeiming
Copy link

[email protected] supports use tree-kill exits complated Electron app for dev mode.

@Ax3l3rator
Copy link
Author

Ax3l3rator commented Jan 12, 2024

Hey, @yejimeiming, huge thanks for your reply, your advice partly fixed my problem, looking forward for vite-plugin-electron support tree-kill natively! Closing issue.

@Ax3l3rator
Copy link
Author

Ax3l3rator commented Jan 14, 2024

Reopening, installing tree-kill didn't help, console process is still up when i close window in dev mode(if i don't send SIGKILL manually)

@Ax3l3rator Ax3l3rator reopened this Jan 14, 2024
@yejimeiming
Copy link

yejimeiming commented Feb 9, 2024

You can upgrade to v0.28.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants