当前位置:网站首页>vite 多页面应用刷新页面时,不会在当前路由中,会返回到根路由
vite 多页面应用刷新页面时,不会在当前路由中,会返回到根路由
2022-07-30 14:48:00 【Zheng One Dream】
当我们用 vite 开发多页面应用的时候
如果在另一个页面中存在路由,那么我们刷新页面就会有问题
vite 并没有类似 webpack 的 serverFallback 方法
比如:
当我们访问 /nested/ 它是对的,它会到 /nested 项目
当我们访问 /nested/getInfo/xx 等路由的时候,刷新页面会发现,页面会回到 根项目,而不是当前项目
我们没发配置静态路径回滚,当前我的解决方法是:
plugins: [
{
name: "rewrite-middleware",
configureServer(serve) {
serve.middlewares.use((req, res, next) => {
if (req.url.startsWith("/nested/")) {
if (req.url.includes("src")) {
req.url = req.url
} else {
req.url = "/nested/"
}
}
next()
})
},
}
]加一个 plugin 将 路径强制导到 /nested/ 项目下
但是问题是,他有静态资源,比如我写的那个 includes('src'),如果他有多个静态资源怎么办?
我没法判断是路由还是静态资源,当前写了一个 issue 会持续跟进解决这个问题,找到最好的解决方法
以上是我个人的解决思路
=======================================
官方人员给了我一些建议,现在最好的解决方法,只能是封装成一个函数了(我能想到的最好方法)
我将我的 plugin 封装成了一个可以接收参数的函数,这样起码用起来感觉还是挺漂亮的
const rewritePlugin = (path, excludes) => ({
name: "rewrite-middleware",
configureServer(serve) {
serve.middlewares.use((req, res, next) => {
if (req.url.startsWith(path)) {
const isRaw = excludes.some((dir) => req.url.includes(dir))
if (!isRaw) req.url = path
}
next()
})
},
})
// 第二个参数,看你的项目中有哪些文件夹,都写上就可以了
plugins: [
rewritePlugin('/nested/', ['/nested/src', '/nested/static'])
]边栏推荐
- Sentinel
- MongoDB启动报错 Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
- 编译、链接 - 笔记 - 3
- golang modules initialization project
- 数字量输入模块io
- JHM:芳环羟化双加氧酶数据库DARHD建立及相关引物评价
- 952. 按公因数计算最大组件大小 : 枚举质因数 + 并查集运用题
- 【云原生】灰度发布、蓝绿发布、滚动发布、灰度发布解释
- Extremely Knowing v2 Analysis
- GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
猜你喜欢
随机推荐
Could not acquire management access for administration
5G-based Warehousing Informatization Solution 2022
Distributed pre-course: MySQL implements distributed locks
使用 protobuf 进行数据序列化
分布式前修课:MySQL实现分布式锁
Local Transactions vs Distributed Transactions
数字量输入模块io
QIIME2得到PICRUSt2结果后如何分析
Kubernetes应用管理深度剖析
B+树索引页大小是如何确定的?
针对 MySQL/InnoDB 刷盘调优
Sleuth+Zipkin(可视化) 服务链路追踪
[Cloud native] Grayscale release, blue-green release, rolling release, grayscale release explanation
智能合约安全——私有数据访问
Normal and escaped strings for postgresql
How is the B+ tree index page size determined?
被捧上天的Scrum敏捷管理为何不受大厂欢迎了?
Lock wait timeout exceeded solution
Sleuth+Zipkin (visualization) service link tracking
MySql报错:SqlError(Unable to execute query“, “Can‘t create/write to file OS errno 2 - No such file...








