简体   繁体   English

为什么 React Webpack 生产版本显示空白页面?

[英]Why is React Webpack production build showing Blank page?

I'm building a react app, and at the moment webpack-dev-server works just fine( the hello world text shows up ), But webpack -p shows blank page.我正在构建一个webpack-dev-server应用程序,目前webpack-dev-server工作正常(显示hello world文本),但 webpack -p 显示空白页面。 For the Production build The network tab under chrome dev tools, shows index.html and index_bundle.js to have size 0 B(see picture)对于生产构建 Chrome 开发工具下的网络选项卡,显示index.htmlindex_bundle.js大小为 0 B(见图) 在此处输入图片说明 But That is clearly not the case HTML file size is 227 B & index_bundle.js file size is 195Kb(see picture)但显然情况并非如此 HTML 文件大小为 227 B & index_bundle.js文件大小为 195Kb(见图)

Also Chrome Devtools Elements Tab shows the following(see picture) Chrome Devtools Elements Tab 还显示了以下内容(见图) 在此处输入图片说明

My webpack config file looks like this:我的 webpack 配置文件如下所示: 在此处输入图片说明

I figured it out, I was using browserHistory without setting up a local server.我想通了,我在没有设置本地服务器的情况下使用 browserHistory。 If i changed it to hashHistory it worked.如果我将其更改为 hashHistory,它会起作用。 To test webpack production locally with react-router browser history i needed to do this Configure a Server:要使用 react-router 浏览器历史记录在本地测试 webpack 生产,我需要这样做 配置服务器:

Your server must be ready to handle real URLs.您的服务器必须准备好处理真实的 URL。 When the app first loads at / it will probably work, but as the user navigates around and then hits refresh at /accounts/23 your web server will get a request to /accounts/23.当应用程序第一次在 / 加载时,它可能会工作,但是当用户浏览并在 /accounts/23 刷新时,您的 Web 服务器将收到对 /accounts/23 的请求。 You will need it to handle that URL and include your JavaScript application in the response.您将需要它来处理该 URL 并在响应中包含您的 JavaScript 应用程序。

An express app might look like this: Express 应用程序可能如下所示:

const express = require('express')
const path = require('path')
const port = process.env.PORT || 8080
const app = express()

// serve static assets normally
app.use(express.static(__dirname + '/public'))

// handle every other route with index.html, which will contain
// a script tag to your application's JavaScript file(s).
app.get('*', function (request, response){
  response.sendFile(path.resolve(__dirname, 'public', 'index.html'))
})

app.listen(port)
console.log("server started on port " + port)

And just in case anyone is deploying to firebase using react-router with browser-history do this:以防万一有人使用带有浏览器历史记录的 react-router 部署到 firebase,请执行以下操作:

{
  "firebase": "<YOUR-FIREBASE-APP>",
  "public": "<YOUR-PUBLIC-DIRECTORY>",
  "ignore": [
    "firebase.json",
    "**/.*",
    "**/node_modules/**"
  ],
  "rewrites": [
    {
      "source": "**",
      "destination": "/index.html"
    }
  ]
}

use使用

import { HashRouter } from 'react-router-dom';

instead of而不是

import { BrowserRouter} from 'react-router-dom';

and don't forget to replace it in your routes code并且不要忘记在您的路线代码中替换它

<HashRouter>
  ...
</HashRouter>

Changing改变

import {BrowserRouter as Router} from 'react-router-dom'

to:到:

import {HashRouter as Router} from 'react-router-dom'

in App.js helped me fix the same issue in my create-react-app project.App.js 中帮助我解决了 create-react-app 项目中的相同问题。

GitHub Pages doesn't support routers that use the HTML5 pushState history API under the hood (for example, React Router using browserHistory ). GitHub Pages 不支持在pushState使用 HTML5 pushState history API 的路由器(例如,使用browserHistory React Router)。 This is because when there is a fresh page load for a url like http://user.github.io/todomvc/todos/42 , where /todos/42 is a frontend route, the GitHub Pages server returns 404 because it knows nothing of /todos/42 .这是因为当有一个像http://user.github.io/todomvc/todos/42这样的 url 的新页面加载时,其中/todos/42是前端路由,GitHub Pages 服务器返回 404,因为它什么都不知道的/todos/42 If you want to add a router to a project hosted on GitHub Pages, here are a couple of solutions:如果您想将路由器添加到托管在 GitHub Pages 上的项目,这里有几个解决方案:

  • You could switch from using HTML5 history API to routing with hashes.您可以从使用 HTML5 历史 API 切换到使用哈希路由。 If you use React Router, you can switch to hashHistory for this effect, but the URL will be longer and more verbose (for example, http://user.github.io/todomvc/#/todos/42?_k=yknaj ).如果你使用 React Router,你可以切换到hashHistory来实现这个效果,但是 URL 会更长更冗长(例如http://user.github.io/todomvc/#/todos/42?_k=yknaj ) . Read more about different history implementations in React Router.阅读更多关于 React Router 中不同历史实现的信息。
  • Alternatively, you can use a trick to teach GitHub Pages to handle 404 by redirecting to your index.html page with a special redirect parameter.或者,您可以使用一个技巧来教 GitHub Pages 处理 404,方法是使用特殊的重定向参数重定向到您的index.html页面。 You would need to add a 404.html file with the redirection code to the build folder before deploying your project, and you'll need to add code handling the redirect parameter to index.html .在部署项目之前,您需要将带有重定向代码的404.html文件添加到build文件夹,并且您需要将处理重定向参数的代码添加到index.html You can find a detailed explanation of this technique in this guide .您可以在本指南中找到有关此技术的详细说明。
<BrowserRouter basename="/calendar" />
<Link to="/today"/> // renders <a href="/calendar/today">

basename : string基本名称:字符串
The base URL for all locations.所有位置的基本 URL。 If your app is served from a sub-directory on your server, you'll want to set this to the sub-directory.如果您的应用程序是从服务器上的子目录提供的,您需要将其设置为子目录。 A properly formatted basename should have a leading slash, but no trailing slash.格式正确的基本名称应该有一个前导斜杠,但没有尾随斜杠。

None of these worked for me my error was because I mixed up folders这些都不适合我我的错误是因为我混淆了文件夹

Solution:解决方法:

npm run build

my apache config:我的 apache 配置:

did not work没用

DocumentRoot /var/www/sites/example.com/client/public/

worked (build folder not public)工作(构建文件夹不公开)

DocumentRoot /var/www/sites/example.com/client/build/

I had a unique solution - only one page was not running for me.我有一个独特的解决方案 - 只有一页没有为我运行。 So I just inspected the console log and found the error.所以我只是检查了控制台日志并发现了错误。 For some reason, the error was only displaying for "react-scripts-build", not "react-scripts-start".出于某种原因,错误仅显示为“react-scripts-build”,而不是“react-scripts-start”。

This resulted in a blank page这导致了一个空白页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM