提交 7a96ac22 编写于 作者: fxy060608's avatar fxy060608

fix(h5): history mode

上级 8ea1aa04
......@@ -52,3 +52,10 @@ export function getUniStatistics(inputDir: string, platform: UniApp.PLATFORM) {
manifest[platform] && manifest[platform].uniStatistics
)
}
export function getRouterOptions(manifestJson: Record<string, any>): {
mode?: 'history' | 'hash'
base?: string
} {
return extend({}, manifestJson.h5?.router)
}
import fs from 'fs'
import path from 'path'
import { parse as parseUrl } from 'url'
import {
isInHBuilderX,
resolveMainPathOnce,
UniVitePlugin,
getRouterOptions,
parseManifestJsonOnce,
EXTNAME_VUE_RE,
} from '@dcloudio/uni-cli-shared'
import { createHandleHotUpdate } from './handleHotUpdate'
import { createTransformIndexHtml } from './transformIndexHtml'
......@@ -96,6 +100,37 @@ export const UniH5Plugin: UniVitePlugin = {
}
return res
}
const routerOptions = getRouterOptions(
parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
)
if (routerOptions.mode === 'history') {
server.middlewares.use(async (req, res, next) => {
// 当页面被作为组件引用时,会导致history刷新该页面直接显示js代码,因为该页面已被缓存为了module,
// https://github.com/vitejs/vite/blob/702d50315535c189151c67d33e4a22124f926bed/packages/vite/src/node/server/transformRequest.ts#L52
// /pages/tabBar/API/API
let { url } = req
if (url) {
const base = server.config.base
const parsed = parseUrl(url)
let newUrl = url
if ((parsed.pathname || '/').startsWith(base)) {
newUrl = newUrl.replace(base, '/')
}
if (
!path.extname(newUrl) &&
!newUrl.endsWith('/') &&
!newUrl.includes('?')
) {
const module = await server.moduleGraph.getModuleByUrl(newUrl)
if (module && module.file && EXTNAME_VUE_RE.test(module.file)) {
// /pages/tabBar/API/API => /pages/tabBar/API/API?__t__=time
req.url = url + '?__t__=' + Date.now()
}
}
}
next()
})
}
},
handleHotUpdate: createHandleHotUpdate(),
transformIndexHtml: createTransformIndexHtml(),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册