swpp.config.ts
· 4.3 KiB · TypeScript
原始檔案
import {
defineConfig
} from 'swpp-backends'
defineConfig({
compilationEnv: {
DOMAIN_HOST: new URL('https://blog.liushen.fun'),
SERVICE_WORKER: "sw",
JSON_HTML_LIMIT: 10,
// isStable: (url: URL) => {
// return [
// /^(https?:\/\/|\/\/)(cdn|fastly)\.jsdelivr\.net\/npm\/.*@\d+\.\d+\.\d+\//,
// /^(https?:\/\/|\/\/)jsd\.liiiu\.cn\/.*@\d+\.\d+\.\d+\//,
// /^(https?:\/\/|\/\/)cdn\.jsdmirror\.com\/.*@\d+\.\d+\.\d+\//,
// /^(https?:\/\/|\/\/)cdn\.staticfile\.org\/.*\/\d+\.\d+\.\d+\//,
// /^(https?:\/\/|\/\/)lf\d+-cdn-tos\.bytecdntp\.com\/.*\/\d+\.\d+\.\d+\//,
// /^(https?:\/\/|\/\/)npm\.elemecdn\.com\/.*@\d+\.\d+\.\d+\//
// ].some(it => it.test(url.href))
// },
VERSION_LENGTH_LIMIT: 256,
// NETWORK_FILE_FETCHER: {
// referer: "https://blog.liushen.fun",
// getStandbyList(url: string | URL): (string | URL)[] {
// if (typeof url === 'string') url = new URL(url)
// if (url.hostname === 'npm.elemecdn.com') {
// return [`https://fastly.jsdelivr.net${url.pathname}`]
// }
// return [url]
// }
// }
},
domConfig: {
onSuccess: () => {
caches
.match("https://id.v3/")
.then((res) => {
if (res) {
res.json().then((json) => {
btf && btf.snackbarShow(`已自动刷新SW缓存,当前版本为 v${json.escape + '.' + '0' + '.' + json.global + '.' + json.local} ,如果不生效,请尝试刷新页面以获取最新内容。`);
});
} else {
console.warn("未找到版本信息缓存,可能是第一次访问或缓存已过期");
}
}).catch((err) => console.warn("获取版本信息缓存失败", err));
}
},
crossEnv: {
CACHE_NAME: "BlogCache",
VERSION_PATH: "https://id.v3/",
ESCAPE: 15,
},
runtimeDep: {
getStandbyRequests: (request: Request): {t: number, l: () => Request[]} | void => {
const srcUrl = request.url
const {host, pathname} = new URL(srcUrl)
// noinspection SpellCheckingInspection
const commonCdnList = ['jsd.liiiu.cn', 'cdn.jsdmirror.com', 'fastly.jsdelivr.net']
// noinspection SpellCheckingInspection
const elme = 'npm.elemecdn.com'
const urlMapper = (it: string) => new Request(it, request)
if (host === elme) {
return {
t: 2000,
l: () => [...commonCdnList.map(it => `https://${it}/npm${pathname}`)].map(urlMapper)
}
}
if (host === 'jsd.liiiu.cn') {
commonCdnList.splice(0, 1)
return {
t: 2000,
l: () => [...commonCdnList.map(it => `https://${it}${pathname}`)].map(urlMapper)
}
}
}
},
crossDep: {
matchCacheRule: {
runOnBrowser: (url: URL) => {
let { host, pathname } = url;
// 处理省略index.html的情况
if (pathname.endsWith('/')) pathname += 'index.html';
// 仅仅对于blog.liushen.fun 处理 html 和 json
if (host.endsWith('blog.liushen.fun')) {
if (pathname.endsWith('.json')) return 3600000; // 1 hour
if (pathname.endsWith('.html')) return false; // 暂不缓存
if (pathname.endsWith('.webp') || pathname.endsWith('.jpg') || pathname.endsWith('.png')) return 432000000; // 5 days
}
if (/\.(js|css|woff2|woff|ttf|cur)$/.test(url.pathname)) return 864000000; // 10 days
// return ((url.host.endsWith('blog.liushen.fun') && /(\/|\.json)$/.test(url.pathname)) || /\.(js|css|woff2|woff|ttf|cur)$/.test(url.pathname)) ? 86400000 : false
},
runOnNode(url: URL) {
// @ts-ignore
return this.runOnBrowser(url)
}
}
},
})
| 1 | import { |
| 2 | defineConfig |
| 3 | } from 'swpp-backends' |
| 4 | |
| 5 | defineConfig({ |
| 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 | }) |