You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uniTimestampMiddleware = void 0;
const url_1 = require("url");
const path_1 = __importDefault(require("path"));
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
function uniTimestampMiddleware(server) {
return async function timestampMiddleware(req, _, 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 = (0, url_1.parse)(url);
let newUrl = url;
if ((parsed.pathname || '/').startsWith(base)) {
newUrl = newUrl.replace(base, '/');
}
if (!path_1.default.extname(newUrl) &&
!newUrl.endsWith('/') &&
!newUrl.includes('?')) {
const module = await server.moduleGraph.getModuleByUrl(newUrl);
if (module && module.file && uni_cli_shared_1.EXTNAME_VUE_RE.test(module.file)) {
// /pages/tabBar/API/API => /pages/tabBar/API/API?__t__=time
req.url = url + '?__t__=' + Date.now();
}
}
}
next();
};
}
exports.uniTimestampMiddleware = uniTimestampMiddleware;