feat: 支持history模式下请求html文件404时fallback到/index.html

This commit is contained in:
万纯 2021-03-29 10:48:09 +08:00
parent a9a615a09b
commit 19108db7ce
5 changed files with 23 additions and 6 deletions

View File

@ -65,6 +65,7 @@
"style-loader": "^2.0.0",
"url-loader": "^4.1.1",
"vue-loader": "^16.1.2",
"connect-history-api-fallback": "^1.6.0",
"webpack": "^5.24.2",
"webpack-bundle-analyzer": "^4.4.0",
"webpack-chain": "^6.5.1",

View File

@ -0,0 +1,16 @@
import { extname, join } from 'path';
import historyFallback from 'connect-history-api-fallback';
const ASSET_EXTNAMES = ['.ico', '.png', '.jpg', '.jpeg', '.gif', '.svg'];
export default () => (req, res, next) => {
if (req.path === '/favicon.ico') {
res.sendFile(join(__dirname, 'fes.png'));
} else if (ASSET_EXTNAMES.includes(extname(req.path))) {
next();
} else {
const history = historyFallback();
history(req, res, next);
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -40,6 +40,7 @@ export default (api) => {
getBundleAndConfigs
} = require('../buildDevUtils');
const { delay } = require('@umijs/utils');
const createRouteMiddleware = require('./createRouteMiddleware').default;
const generateFiles = require('../../../utils/generateFiles').default;
const { watchPkg } = require('./watchPkg');
@ -175,7 +176,7 @@ export default (api) => {
port,
proxy: api.config.proxy,
https: isHTTPS,
beforeMiddlewares,
beforeMiddlewares: [...beforeMiddlewares, createRouteMiddleware()],
afterMiddlewares: [...middlewares],
customerDevServerConfig: api.config.devServer
});

View File

@ -2,17 +2,16 @@
export default {
// base: '',
qiankun: {
micro: {}
},
base: '/base/',
define: {
__DEV__: false
},
html: {
title: '海贼王'
},
publicPath: '/',
router: {
mode: 'history'
},
access: {
roles: {
admin: ["/", "/store", "https://www.baidu.com"]