feat(cdn): cdn功能优化
这个提交包含在:
@@ -5,7 +5,7 @@ const pwaCdnConfig = {
|
||||
cdnUrl: 'https://cdn.fillcode.com/',
|
||||
serviceWorkerUrl: '/__static/sw-cdn.js',
|
||||
staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/,
|
||||
debug: true,
|
||||
debug: false,
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@ let config = {
|
||||
cdnUrl: 'https://cdn.fillcode.com/',
|
||||
serviceWorkerUrl: '/__static/sw-cdn.js',
|
||||
staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/,
|
||||
debug: true,
|
||||
debug: false,
|
||||
}
|
||||
|
||||
// 监听配置更新消息
|
||||
@@ -62,20 +62,6 @@ async function handleStaticResource(request, url) {
|
||||
|
||||
if (!response.ok) throw new Error('Target domain response not ok: ' + response.status)
|
||||
|
||||
// 修复重定向问题
|
||||
if([301, 302, 307, 308].includes(response.status)) {
|
||||
const location = response.headers.get('location')
|
||||
|
||||
// 如果重定向的地址不是CDN地址,或者已经包含了CDN子路径,则返回原始响应
|
||||
if(!location?.startsWith('/') || location.startsWith(`/${cdnPath}`)) return response
|
||||
|
||||
// 否则,重定向到新的CDN地址
|
||||
const redirectUrl = new URL(location, config.cdnUrl + cdnPath)
|
||||
|
||||
if (config.debug) console.log('PWA-CDN: Redirecting to', redirectUrl.href)
|
||||
return Response.redirect(redirectUrl.href, response.status)
|
||||
}
|
||||
|
||||
return response
|
||||
} catch (error) {
|
||||
if (config.debug) console.warn('PWA-CDN: Fallback to original request for', url.href, error)
|
||||
|
||||
在新议题中引用
屏蔽一个用户