【问题标题】:Electron shows blank screen when running in development yet works in productionElectron 在开发中运行但在生产中运行时显示空白屏幕
【发布时间】:2020-06-08 19:18:27
【问题描述】:

当我在 CLI 中运行“yarn run electron-dev”时,在浏览器中打开http://localhost:3000 显示主页,但电子应用显示空白。

与其他人在生产中遇到问题但在开发中工作的类似问题不同,我遇到了相反的问题。

查看开发工具,浏览器控制台中没有错误,即使我执行 console.log(),控制台中也没有出现任何错误。

electron.js

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const edge = require('electron-edge-js');
const path = require('path');
const isDev = require('electron-is-dev');
require('electron-reload')

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({
      width: 900, 
      height: 680,
      webPreferences: {
        nodeIntegration: true,
      },
  });
  console.log('isDev', isDev);
  mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);
  if (isDev) {
    // Open the DevTools.
    //BrowserWindow.addDevToolsExtension('<location to your react chrome extension>');
    mainWindow.webContents.openDevTools();
  }

  mainWindow.on('closed', () => mainWindow = null);
}

app.on('ready', createWindow);

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

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow();
    console.log('entering func123');
    func123();
  }
});

包.json

{
  "name": "myelectronapp",
  "description": "Electron + Create React App + Electron Builder",
  "version": "0.1.0",
  "private": true,
  "author": {
    "name": "Andrew C",
    "email": "your.email@domain.com",
    "url": "https://your-website.com"
  },
  "build": {
    "appId": "com.andrewchoi.my-electron-app",
    "productName": "MyElectronApp",
    "copyright": "Copyright © 2019 ${author}",
    "mac": {
      "category": "public.app-category.utilities"
    },
    "files": [
      "build/**/*",
      "node_modules/**/*"
    ],
    "directories": {
      "buildResources": "assets"
    }
  },
  "dependencies": {
    "@material-ui/core": "^4.9.4",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "electron-edge-js": "^12.8.1",
    "electron-is-dev": "^1.1.0",
    "mssql-connection-string": "^0.3.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "3.4.0"
  },
  "main": "public/electron.js",
  "homepage": "./",
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "rescripts test",
    "eject": "react-scripts eject",
    "electron-dev": "concurrently \"cross-env BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\"",
    "postinstall": "electron-builder",
    "preelectron-pack": "yarn build",
    "electron-pack": "electron-builder build -w"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@rescripts/cli": "^0.0.13",
    "@rescripts/rescript-env": "^0.0.11",
    "concurrently": "^5.1.0",
    "cross-env": "^7.0.0",
    "electron": "8.0.1",
    "electron-builder": "22.3.5",
    "wait-on": "^4.0.0"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

【问题讨论】:

  • 介意分享您的index.html 文件。在基本 href 之前可能缺少 .
  • 当我从 electron.js 中删除电子重载时,它对我有用。 electron-reload 未列在您的 package.json 中。我也遇到了电子边缘 js 的问题。如果我删除这两个依赖项,我可以在 localhost:3000 上加载一个空白 index.html 文件。
  • @PrameshBajracharya 我为这个应用程序使用了 create-react-app 并且没有更改这些文件,所以我认为这不是问题,但我现在已经将它包含在问题中.
  • @narmageddon 我尝试删除两个依赖项 electron-reload 和 electron-edge-js 但我仍然得到空白的电子应用程序

标签: javascript node.js reactjs electron create-react-app


【解决方案1】:

这对我来说很奇怪:

mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);

是否有理由为客户端内容使用单独的服务器,而不是像在 prod 中那样仅将索引文件作为文件资源传递?

检查其他服务器是否正在运行。当我尝试加载 web url 但服务器没有运行时,我知道“空白页”效果。

【讨论】:

  • 大概wait-on(参见package.json 中的electron-dev 条目)应该确保端口3000 服务器正在运行。 (但是,我同意,这是“代码气味”最强的行,file:// 和 http:// 之间的不匹配)
  • 我认为它只会让它变得更加复杂,我很高兴能从这种设置中学到好处。我认为,如果您从 dev 中的服务器和 prod 中的文件提供客户端资源,您只需要格外小心包装,并且您可以从客户端脚本中的“电子”中获取所需的一切开发模式。
  • @Andreas_D 我尝试只删除本地主机 url,以便它加载索引文件,但它仍然无法正常工作。
  • 我面临着和他一样的问题。我只想提一下为什么他加载 URL 而不是加载索引文件,是我猜他想在电子环境中开发应用程序,并且每当他更改 backgroundColor 时,他都试图避免前端的构建过程,其中我的情况是构建最多需要 30 秒。
【解决方案2】:

此问题已解决。 我删除了 electron-edge-js 和 electron-reload,并回到使用 npm 而不是 yarn 来运行脚本和安装依赖项等。 现在它适用于开发和生产。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2021-09-03
    • 1970-01-01
    • 2016-09-10
    • 2016-10-19
    相关资源
    最近更新 更多