-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
48 lines (33 loc) · 1.25 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Router from 'vue-router';
import { defaultCacheUrls } from './config';
export const getCache = (fullUrl: string, cacheList = defaultCacheUrls) => {
const parsedUrl = new URL(fullUrl);
const found = cacheList
.find(({ hostname, pathname }) =>
parsedUrl.hostname === hostname && parsedUrl.pathname === pathname
);
return found || null;
}
export const isCacheUrl = (fullUrl: string, cacheList = defaultCacheUrls) => {
const cache = getCache(fullUrl, cacheList);
return !!cache;
};
export const getRealUrl = (fullUrl: string, cacheList = defaultCacheUrls) => {
const cache = getCache(fullUrl, cacheList);
if (!cache || !cache.getRealUrl) return null;
return cache.getRealUrl(fullUrl);
}
export const patchNuxtRouter = (router: Router, href: string) => {
const origRouterResolve = router.resolve.bind(router);
router.resolve = () => {
router.resolve = origRouterResolve;
return origRouterResolve(href);
};
};
export const getFullPath = (fullUrl: string, base = '/') => {
const parsed = new URL(fullUrl);
let fullPath = parsed.pathname + parsed.search + parsed.hash;
if (fullPath.startsWith(base)) fullPath = fullPath.replace(base, '');
if (!fullPath.startsWith('/')) fullPath = '/' + fullPath;
return fullPath;
};