当前位置:网站首页>When the vite multi-page application refreshes the page, it will not be in the current route and will return to the root route
When the vite multi-page application refreshes the page, it will not be in the current route and will return to the root route
2022-07-30 15:43:00 【Zheng One Dream】
When we use vite to develop multi-page applications
If there is a route in another page, then we will have a problem refreshing the page
vite does not have a serverFallback method similar to webpack
For example:
When we visit /nested/ it's right, it goes to the /nested item
When we visit /nested/getInfo/xx and other routes, refresh the page and find that the page will go back to the root project instead of the current project
We did not send the configuration static path rollback, my current solution is:
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()})},}]Add a plugin to force the path to the /nested/ project
But the problem is, he has static resources, such as the includes('src') I wrote, what if he has multiple static resources?
I can't judge whether it is a route or a static resource. I have written an issue and will continue to follow up to solve this problem and find the best solution
The above is my personal solution
==========================================
The official staff gave me some suggestions, the best solution now is to encapsulate it into a function (the best way I can think of)
I encapsulated my plugin into a function that can receive parameters, so at least it feels pretty good to use
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()})},})// The second parameter, see which folders are in your project, just write them allplugins: [rewritePlugin('/nested/', ['/nested/src', '/nested/static'])]边栏推荐
猜你喜欢
随机推荐
Flink实时仓库-DWS层(关键词搜索分析-自定义函数,窗口操作,FlinkSql设置水位线,保存数据到Clickhouse)模板代码
Mysql database query is very slow. Besides the index, what else can be caused?
Distributed pre-course: MySQL implements distributed locks
100w的数据表比1000w的数据表查询更快吗?
481-82(105、24、82、34、153)
tiup clean
Excel uses Visual Basic Editor to modify macros
Flask之路由(app.route)详解
让人上瘾的新一代开发神器,彻底告别Controller、Service、Dao等方法
元宇宙的前景及四大赛道
tiup env
How to do a good job in technology selection
视频加密的误解
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
【云原生】灰度发布、蓝绿发布、滚动发布、灰度发布解释
MySQL客户端工具的使用与MySQL SQL语句
极验深知v2分析
在树莓派上驱动CSI摄像头
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
Delayed message queue








