首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Production Build在页面刷新时显示404

React Production Build在页面刷新时显示404
EN

Stack Overflow用户
提问于 2019-06-18 22:47:27
回答 1查看 2.3K关注 0票数 1

我有一个ReactJS应用程序,它在开发环境中工作得很好。我正在使用webpack。当我运行yarn build并将我的文件放到服务器上时,一切都运行得很好。但是如果我点击浏览器上的刷新,我会得到404。

我的服务器使用Apache。我试过htaccess,我做过historyFallBackApi。他们似乎都不能解决我的问题

这是我的.htaccess

代码语言:javascript
复制
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(assets/?|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

这是我的webpack.config.js

代码语言:javascript
复制
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),
    );

这是我的路线

代码语言:javascript
复制
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;

这是我的自定义历史记录

代码语言:javascript
复制
import { createBrowserHistory } from 'history';

const history = createBrowserHistory();

export default history;

在服务器上页面刷新时,我一直收到404。

EN

回答 1

Stack Overflow用户

发布于 2019-07-10 04:29:24

我会像这样更新你的.htaccess文件。问题可能是您当前用作.htaccess文件的内容不会将404错误重定向到index.html

代码语言:javascript
复制
RewriteEngine On  
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteRule ^ /index.html [L]

我还会添加一个自定义的404页面。为此,您需要创建一个新组件并向交换机添加另一个路由。该路由不应该有路径,并且应该是交换机的最后一个路由。

<Route component={NotFoundComponent} />

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56651771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档