【问题标题】:Error setting up new React project when moving index.html to public folder将 index.html 移动到公用文件夹时设置新的 React 项目时出错
【发布时间】:2017-10-26 22:32:03
【问题描述】:

我一直在努力解决一个看似简单的问题。我正在建立一个新的 React 项目并构建我的文件夹。我有这样的事情:

/node_modules
/public
   |__ 0.bundle.js
   |__ bundle.js
   // the goal is to move index.html in this folder
/src
   |__components
   App.js
   ClientApp.js
.babelrc
.eslintrc
index.html
package.json
server.js
webpack.config.js
yarn.lock

现在我的索引文件看起来就像这样:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>React Test</title>
</head>
<body>
  <div id="app"><%= body %></div>
  <script src="/public/bundle.js"></script>
</body>
</html>

而我的 server.js 是:

require('babel-register')

const express = require('express')
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const ReactRouter = require('react-router-dom')
const StaticRouter = ReactRouter.StaticRouter
const _ = require('lodash')
const fs = require('fs')
const PORT = 5000
const baseTemplate = fs.readFileSync('./index.html')
const template = _.template(baseTemplate)
const App = require('./src/App').default

const server = express()

server.use('/public', express.static('./public'))

server.use((req, res) => {
  const body = ReactDOMServer.renderToString(
    React.createElement(StaticRouter, {location: req.url, context: {}},
      React.createElement(App)
    )
  )

  res.write(template({body: body}))
  res.end()
})

console.log('listening on port', PORT)
server.listen(PORT)

一切都像魅力一样工作(服务器和客户端渲染),现在我想做一个简单的更改并将index.html 文件移动到/public 文件夹中。我更新了指向它的各个文件以及index 正在寻找bundle.js. 文件的位置。所以我的新index.html 是:

...
<body>
  <div id="app"><%= body %></div>
  <!-- <script src="/public/bundle.js"></script> I actually removed this -->
  <script src="bundle.js"></script>
</body>
...

我的服务器更新为:

...
const baseTemplate = fs.readFileSync('./public/index.html')
const template = _.template(baseTemplate)
const App = require('./src/App').default

...
server.listen(PORT)

使用此设置,客户端渲染仍然有效,但服务器端渲染会引发错误:

Uncaught SyntaxError: Unexpected token &lt;

我花了一段时间才查明问题所在。显然server.js 能够在我将文件移动到public 文件夹后检测到文件(如果我检查HTML 它实际上就在那里),但它在加载实际应用程序时遇到了麻烦(意思是js) .我觉得做出这个改变打破了我现在认为理所当然的一些参考。有没有想过在哪里寻找我的错误?

【问题讨论】:

    标签: javascript reactjs webpack server-side-rendering


    【解决方案1】:

    有趣的是,我发现了问题所在,我的配置也有涉及webpack 的问题,我正在跳过。我没有根据我的新配置更新output 路径。

    output: {
      path: path.join(__dirname, '/public'),
      filename: 'bundle.js',
      // publicPath: '/public/' --> This was causing the error
      publicPath: '/js/'
    },
    

    导致错误的原因是,如果 index.html 被放置在主文件夹中(就像原来一样),那么它需要在 public 文件夹中查找旧的包webpack 使用 publicPath 指定的配置。将index.html 移动到public 文件夹后,现在捆绑包将与index.html 文件位于同一文件夹中,因此我们应该引用的路径现在是index 自己的文件夹。这转化为上面 sn-p 中指定的新 publicPath

    我希望这可以帮助其他人,我应该更加了解我的 webpack 配置,我不会遇到这个问题。编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      相关资源
      最近更新 更多