Last active 7 hours ago

SWPP标准配置文件,适配最新正式版

Revision 5df1e3d5cff6ba3a6071e3c10f9a30dd54c97e10

swpp.config.ts Raw
1import {
2 defineConfig
3} from 'swpp-backends'
4
5defineConfig({
6 compilationEnv: {
7 DOMAIN_HOST: new URL('https://blog.liushen.fun'),
8 SERVICE_WORKER: "sw",
9 JSON_HTML_LIMIT: 10,
10 // isStable: (url: URL) => {
11 // return [
12 // /^(https?:\/\/|\/\/)(cdn|fastly)\.jsdelivr\.net\/npm\/.*@\d+\.\d+\.\d+\//,
13 // /^(https?:\/\/|\/\/)jsd\.liiiu\.cn\/.*@\d+\.\d+\.\d+\//,
14 // /^(https?:\/\/|\/\/)cdn\.jsdmirror\.com\/.*@\d+\.\d+\.\d+\//,
15 // /^(https?:\/\/|\/\/)cdn\.staticfile\.org\/.*\/\d+\.\d+\.\d+\//,
16 // /^(https?:\/\/|\/\/)lf\d+-cdn-tos\.bytecdntp\.com\/.*\/\d+\.\d+\.\d+\//,
17 // /^(https?:\/\/|\/\/)npm\.elemecdn\.com\/.*@\d+\.\d+\.\d+\//
18 // ].some(it => it.test(url.href))
19 // },
20 VERSION_LENGTH_LIMIT: 256,
21 // NETWORK_FILE_FETCHER: {
22 // referer: "https://blog.liushen.fun",
23 // getStandbyList(url: string | URL): (string | URL)[] {
24 // if (typeof url === 'string') url = new URL(url)
25 // if (url.hostname === 'npm.elemecdn.com') {
26 // return [`https://fastly.jsdelivr.net${url.pathname}`]
27 // }
28 // return [url]
29 // }
30 // }
31 },
32
33 domConfig: {
34 onSuccess: () => {
35 caches
36 .match("https://id.v3/")
37 .then((res) => {
38 if (res) {
39 res.json().then((json) => {
40 btf && btf.snackbarShow(`已自动刷新SW缓存,当前版本为 v${json.escape + '.' + '0' + '.' + json.global + '.' + json.local} ,如果不生效,请尝试刷新页面以获取最新内容。`);
41 });
42 } else {
43 console.warn("未找到版本信息缓存,可能是第一次访问或缓存已过期");
44 }
45 }).catch((err) => console.warn("获取版本信息缓存失败", err));
46 }
47 },
48
49 crossEnv: {
50 CACHE_NAME: "BlogCache",
51 VERSION_PATH: "https://id.v3/",
52 ESCAPE: 15,
53 },
54
55 runtimeDep: {
56 getStandbyRequests: (request: Request): {t: number, l: () => Request[]} | void => {
57 const srcUrl = request.url
58 const {host, pathname} = new URL(srcUrl)
59 // noinspection SpellCheckingInspection
60 const commonCdnList = ['jsd.liiiu.cn', 'cdn.jsdmirror.com', 'fastly.jsdelivr.net']
61 // noinspection SpellCheckingInspection
62 const elme = 'npm.elemecdn.com'
63 const urlMapper = (it: string) => new Request(it, request)
64 if (host === elme) {
65 return {
66 t: 2000,
67 l: () => [...commonCdnList.map(it => `https://${it}/npm${pathname}`)].map(urlMapper)
68 }
69 }
70 if (host === 'jsd.liiiu.cn') {
71 commonCdnList.splice(0, 1)
72 return {
73 t: 2000,
74 l: () => [...commonCdnList.map(it => `https://${it}${pathname}`)].map(urlMapper)
75 }
76 }
77 }
78 },
79
80 crossDep: {
81 matchCacheRule: {
82 runOnBrowser: (url: URL) => {
83 let { host, pathname } = url;
84
85 // 处理省略index.html的情况
86 if (pathname.endsWith('/')) pathname += 'index.html';
87
88 // 仅仅对于blog.liushen.fun 处理 html 和 json
89 if (host.endsWith('blog.liushen.fun')) {
90 if (pathname.endsWith('.json')) return 3600000; // 1 hour
91 if (pathname.endsWith('.html')) return false; // 暂不缓存
92 if (pathname.endsWith('.webp') || pathname.endsWith('.jpg') || pathname.endsWith('.png')) return 432000000; // 5 days
93 }
94 if (/\.(js|css|woff2|woff|ttf|cur)$/.test(url.pathname)) return 864000000; // 10 days
95
96 // return ((url.host.endsWith('blog.liushen.fun') && /(\/|\.json)$/.test(url.pathname)) || /\.(js|css|woff2|woff|ttf|cur)$/.test(url.pathname)) ? 86400000 : false
97 },
98 runOnNode(url: URL) {
99 // @ts-ignore
100 return this.runOnBrowser(url)
101 }
102 }
103 },
104})