feat(cdn): cdn功能优化

这个提交包含在:
2025-08-15 19:36:38 +08:00 未验证
父节点 2451f98b32
当前提交 405a0d047f
修改 5 个文件,包含 17 行新增37 行删除
+1 -1
查看文件
@@ -44,7 +44,7 @@ spec:
subs_filter_types text/html; subs_filter_types text/html;
subs_filter '<(script|img|link)([^>]*) (src|href)="/' '<$1$2 $3="https://cdn.fillcode.com/dev-cm/' ri; subs_filter '<(script|img|link)([^>]*) (src|href)="/' '<$1$2 $3="https://cdn.fillcode.com/dev-cm/' ri;
subs_filter 'url\(\/' 'url(https://cdn.fillcode.com/dev-cm/' ri; subs_filter 'url\(\/' 'url(https://cdn.fillcode.com/dev-cm/' ri;
subs_filter 'Halo' 'FillCode' r; subs_filter '<head>' '<head><script src="/__static/pwa-cdn.js"></script>' ri;
hostname: dev.cm hostname: dev.cm
path: / path: /
pathType: Prefix pathType: Prefix
+2 -16
查看文件
@@ -316,7 +316,7 @@ data:
cdnUrl: 'https://cdn.fillcode.com/', cdnUrl: 'https://cdn.fillcode.com/',
serviceWorkerUrl: '/__static/sw-cdn.js', serviceWorkerUrl: '/__static/sw-cdn.js',
staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/, staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/,
debug: true, debug: false,
} }
/** /**
@@ -377,7 +377,7 @@ data:
cdnUrl: 'https://cdn.fillcode.com/', cdnUrl: 'https://cdn.fillcode.com/',
serviceWorkerUrl: '/__static/sw-cdn.js', serviceWorkerUrl: '/__static/sw-cdn.js',
staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/, staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/,
debug: true, debug: false,
} }
// 监听配置更新消息 // 监听配置更新消息
@@ -434,20 +434,6 @@ data:
if (!response.ok) throw new Error('Target domain response not ok: ' + response.status) 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 return response
} catch (error) { } catch (error) {
if (config.debug) console.warn('PWA-CDN: Fallback to original request for', url.href, error) if (config.debug) console.warn('PWA-CDN: Fallback to original request for', url.href, error)
+12 -4
查看文件
@@ -6,31 +6,39 @@ metadata:
namespace: infra-net namespace: infra-net
annotations: annotations:
nginx.ingress.kubernetes.io/use-regex: "true" nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2 nginx.ingress.kubernetes.io/rewrite-target: "/$3"
# 重定向配置
nginx.ingress.kubernetes.io/proxy-redirect-from: "/"
nginx.ingress.kubernetes.io/proxy-redirect-to: "/$1/"
# 添加允许跨域请求 # 添加允许跨域请求
nginx.ingress.kubernetes.io/enable-cors: "true" nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-origin: "https://dev.cm, https://*.dev.cm, https://fillcode.cm, https://*.fillcode.cm" nginx.ingress.kubernetes.io/cors-allow-origin: "https://dev.cm, https://*.dev.cm, https://fillcode.cm, https://*.fillcode.cm"
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
nginx.ingress.kubernetes.io/proxy-cookie-domain: "~^(.+)$ cdn.fillcode.com"
nginx.ingress.kubernetes.io/proxy-cookie-path: "~^/(.*)$ /$1"
nginx.ingress.kubernetes.io/configuration-snippet: |
more_set_headers "Set-Cookie $sent_http_set_cookie; SameSite=None; Secure";
spec: spec:
ingressClassName: nginx ingressClassName: nginx
rules: rules:
- host: cdn.fillcode.com - host: cdn.fillcode.com
http: http:
paths: paths:
- path: /dev-cm(/|$)(.*) - path: /(dev-cm)(/|$)(.*)
pathType: ImplementationSpecific pathType: ImplementationSpecific
backend: backend:
service: service:
name: cdn-halo name: cdn-halo
port: port:
number: 80 number: 80
- path: /git-dev-cm(/|$)(.*) - path: /(git-dev-cm)(/|$)(.*)
pathType: ImplementationSpecific pathType: ImplementationSpecific
backend: backend:
service: service:
name: cdn-gitea-http name: cdn-gitea-http
port: port:
number: 3000 number: 3000
- path: /monitor-dev-cm(/|$)(.*) - path: /(monitor-dev-cm)(/|$)(.*)
pathType: ImplementationSpecific pathType: ImplementationSpecific
backend: backend:
service: service:
+1 -1
查看文件
@@ -5,7 +5,7 @@ const pwaCdnConfig = {
cdnUrl: 'https://cdn.fillcode.com/', cdnUrl: 'https://cdn.fillcode.com/',
serviceWorkerUrl: '/__static/sw-cdn.js', serviceWorkerUrl: '/__static/sw-cdn.js',
staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/, staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/,
debug: true, debug: false,
} }
/** /**
+1 -15
查看文件
@@ -5,7 +5,7 @@ let config = {
cdnUrl: 'https://cdn.fillcode.com/', cdnUrl: 'https://cdn.fillcode.com/',
serviceWorkerUrl: '/__static/sw-cdn.js', serviceWorkerUrl: '/__static/sw-cdn.js',
staticRegex: /\.(js|css|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|ico)$/, 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 (!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 return response
} catch (error) { } catch (error) {
if (config.debug) console.warn('PWA-CDN: Fallback to original request for', url.href, error) if (config.debug) console.warn('PWA-CDN: Fallback to original request for', url.href, error)