fix: 图片预览错误返回可读 503 + feat: PWA 支持(manifest/sw/图标)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
export default defineNuxtPlugin(() => {
|
||||||
|
if (typeof navigator !== 'undefined' && 'serviceWorker' in navigator) {
|
||||||
|
navigator.serviceWorker.register('/sw.js').catch(() => {})
|
||||||
|
}
|
||||||
|
})
|
||||||
+9
-1
@@ -8,7 +8,15 @@ export default defineNuxtConfig({
|
|||||||
title: 'BT 种子预览台',
|
title: 'BT 种子预览台',
|
||||||
meta: [
|
meta: [
|
||||||
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
|
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
|
||||||
{ name: 'description', content: '搜索 / 解析 / 预览 BT 种子内容,并投递到本机下载工具' }
|
{ name: 'description', content: '搜索 / 解析 / 预览 BT 种子内容,并投递到本机下载工具' },
|
||||||
|
{ name: 'theme-color', content: '#0f0f11' },
|
||||||
|
{ name: 'apple-mobile-web-app-capable', content: 'yes' },
|
||||||
|
{ name: 'apple-mobile-web-app-status-bar-style', content: 'black-translucent' },
|
||||||
|
{ name: 'apple-mobile-web-app-title', content: 'BT 预览台' }
|
||||||
|
],
|
||||||
|
link: [
|
||||||
|
{ rel: 'manifest', href: '/manifest.json' },
|
||||||
|
{ rel: 'apple-touch-icon', href: '/icon.svg', sizes: 'any' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
|
||||||
|
<rect width="512" height="512" rx="96" fill="#0f0f11"/>
|
||||||
|
<text x="256" y="330" font-family="system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif" font-size="220" font-weight="700" text-anchor="middle" fill="#00d9a3">BT</text>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 315 B |
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "BT 种子预览台",
|
||||||
|
"short_name": "BT 预览台",
|
||||||
|
"description": "搜索 / 解析 / 预览 BT 种子内容,并投递到本机下载工具",
|
||||||
|
"start_url": "/",
|
||||||
|
"scope": "/",
|
||||||
|
"display": "standalone",
|
||||||
|
"orientation": "portrait-primary",
|
||||||
|
"background_color": "#0f0f11",
|
||||||
|
"theme_color": "#0f0f11",
|
||||||
|
"icons": [
|
||||||
|
{
|
||||||
|
"src": "/icon.svg",
|
||||||
|
"sizes": "any",
|
||||||
|
"type": "image/svg+xml",
|
||||||
|
"purpose": "any maskable"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
const CACHE_NAME = 'bt-preview-v1'
|
||||||
|
const SHELL = ['/', '/gate', '/settings', '/manifest.json', '/icon.svg']
|
||||||
|
|
||||||
|
self.addEventListener('install', (event) => {
|
||||||
|
event.waitUntil(
|
||||||
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(SHELL)).then(() => self.skipWaiting())
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
self.addEventListener('activate', (event) => {
|
||||||
|
event.waitUntil(self.clients.claim())
|
||||||
|
})
|
||||||
|
|
||||||
|
self.addEventListener('fetch', (event) => {
|
||||||
|
const { request } = event
|
||||||
|
const url = new URL(request.url)
|
||||||
|
|
||||||
|
// API、图片预览、下载等动态请求走网络,不做缓存
|
||||||
|
if (url.pathname.startsWith('/api/')) return
|
||||||
|
|
||||||
|
event.respondWith(
|
||||||
|
caches.match(request).then((cached) => {
|
||||||
|
if (cached) return cached
|
||||||
|
return fetch(request).then((res) => {
|
||||||
|
if (request.method === 'GET' && res.status === 200 && res.type === 'basic') {
|
||||||
|
const clone = res.clone()
|
||||||
|
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone)).catch(() => {})
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
@@ -47,6 +47,7 @@ export default defineEventHandler(async (event) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const settings = await loadSettings()
|
const settings = await loadSettings()
|
||||||
|
try {
|
||||||
const bytes = await withSwarm(infoHash, magnet, settings.metadataTimeout, async (torrent) => {
|
const bytes = await withSwarm(infoHash, magnet, settings.metadataTimeout, async (torrent) => {
|
||||||
const target = (torrent.files || []).find(
|
const target = (torrent.files || []).find(
|
||||||
(f: any) => normPath(String(f.path || f.name || '')) === normPath(filePath)
|
(f: any) => normPath(String(f.path || f.name || '')) === normPath(filePath)
|
||||||
@@ -80,4 +81,11 @@ export default defineEventHandler(async (event) => {
|
|||||||
)
|
)
|
||||||
setHeader(event, 'cache-control', 'private, max-age=3600')
|
setHeader(event, 'cache-control', 'private, max-age=3600')
|
||||||
return Buffer.from(bytes)
|
return Buffer.from(bytes)
|
||||||
|
} catch (err) {
|
||||||
|
// 把 withSwarm 抛出的原始错误(如 webtorrent 无法加载)转成可读的 HTTP 错误,
|
||||||
|
// 避免前端看到 Nitro 的通用 Server Error 页面。
|
||||||
|
if (err && typeof err === 'object' && 'statusCode' in err) throw err
|
||||||
|
const message = err instanceof Error ? err.message : '从做种节点获取图片失败'
|
||||||
|
throw createError({ statusCode: 503, statusMessage: message })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user