vite-plugin-electron icon indicating copy to clipboard operation
vite-plugin-electron copied to clipboard

`vue-router` causes white screen and hot restart failure | 白屏、热重启失败.

Open jsxiaosi opened this issue 11 months ago • 2 comments

我在示例项目electron-vite-vue中添加[email protected]后会,每次修改热更新主线程文件时会停止运行并保留旧的实例

复现代码: src/route/index.ts

import { createRouter, createWebHistory } from 'vue-router';
import type { App } from 'vue';

export const router = createRouter({
  history: createWebHistory(''),
  routes: [],
});

export const configMainRouter = async (app: App<Element>) => {
  app.use(router);
  await router.isReady();
};

src/main.ts

import { router } from './router'

createApp(App)
  .use(router)
  .mount('#app')
  .$nextTick(() => {
    postMessage({ payload: 'removeLoading' }, '*')
  })

复现demo

jsxiaosi avatar Mar 12 '24 14:03 jsxiaosi

没懂你的意思,如果是主进程被修改了,不是会自动重启一次 Electron 吗? 相应的渲染进程会被重新加载一次,怎么会有旧的实例?

问题就在这里,加了路由之后不会重启实例,重启会被终止,旧实例还存在不会关闭。 app.requestSingleInstanceLock 会返回false

复现demo

jsxiaosi avatar Mar 13 '24 12:03 jsxiaosi

window.addEventListener("beforeunload", () => {});

如果在 window 上监听了 beforeunload 事件,自动重启时,旧实例会退出失败。vue-router 里就是监听了这个事件 暂时没空去找根本原因,就先用临时办法绕过了

// electron/main/index.ts
app.on('before-quit', () => {
  if (import.meta.env.DEV) {
    app.exit();
  }
})

ntdiary avatar Mar 15 '24 02:03 ntdiary