我有一个 ReactJS 应用程序,它在开发环境中运行良好。我正在使用 webpack。当我运行 yarn build 并将我的文件放到我的服务器上时,一切都运行良好。但是如果我在浏览器上点击刷新,我会得到一个 404。
我的服务器使用 Apache。我试过htaccess,我做过historyFallBackApi。他们似乎都没有解决我的问题
这是我的 .htaccess
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(assets/?|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
这是我的 webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
const modeConfig = env => require(`./config/webpack.${env}`)(env);
const webpackMerge = require('webpack-merge');
const path = require('path');
module.exports = (
{ mode } = { mode: 'development', presets: [] },
) =>
// console.log(`mode: ${mode}`);
webpackMerge(
{
mode,
entry: './src/index.js',
resolve: {
extensions: ['.js', '.jsx', '.css', 'scss'],
},
devServer: {
historyApiFallback: { index: '/' },
contentBase: './',
open: true,
port: 4100,
},
module: {
rules: [
{
test: /\.(png|jpg|jpeg|gif|ico)$/,
exclude: /node_modules/,
loader: 'url-loader?limit=8192',
},
{
test: /\.(js|jsx|mjs)$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(woff|woff2|eot|ttf)$/,
loader: 'url-loader?limit=100000',
},
{
test: /\.svg$/,
loader: 'svg-inline-loader?classPrefix',
},
],
},
output: {
publicPath: '/',
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js',
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
}),
// new FaviconsWebpackPlugin({ logo: "./public/image.png" })
],
},
modeConfig(mode),
);
这是我的路线
function App() {
return (
<Router history={history}>
<Switch>
<Route exact path="/" component={LoginComponent} />
<Route path="/reset-password" component={ResetPassword} />
<Route path="/dashboard" component={Dashboard} />
<Route path="/cards" component={CardsList} />
<Route path="/view-card" component={ViewCard} />
<Route path="/transactions" component={Transfer} />
<Route path="/users" component={UsersList} />
<Route path="/edit-user" component={EditUser} />
</Switch>
</Router>
);
}
export default App;
这是我的自定义历史
import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
export default history;
我在服务器上的页面刷新时不断收到 404。