File tree Expand file tree Collapse file tree 3 files changed +47
-15
lines changed
Expand file tree Collapse file tree 3 files changed +47
-15
lines changed Original file line number Diff line number Diff line change @@ -14,12 +14,19 @@ const inter = Inter({ subsets: ['latin'] });
1414// 动态生成 metadata,支持配置更新后的标题变化
1515export async function generateMetadata ( ) : Promise < Metadata > {
1616 let siteName = process . env . SITE_NAME || 'KatelyaTV' ;
17- if (
18- process . env . NEXT_PUBLIC_STORAGE_TYPE !== 'd1' &&
19- process . env . NEXT_PUBLIC_STORAGE_TYPE !== 'upstash'
20- ) {
21- const config = await getConfig ( ) ;
22- siteName = config . SiteConfig . SiteName ;
17+
18+ try {
19+ // 只有在非 d1 和 upstash 存储类型时才尝试获取配置
20+ if (
21+ process . env . NEXT_PUBLIC_STORAGE_TYPE !== 'd1' &&
22+ process . env . NEXT_PUBLIC_STORAGE_TYPE !== 'upstash'
23+ ) {
24+ const config = await getConfig ( ) ;
25+ siteName = config . SiteConfig . SiteName ;
26+ }
27+ } catch ( error ) {
28+ // 如果配置获取失败,使用默认站点名称
29+ // siteName 已经有默认值,不需要额外处理
2330 }
2431
2532 return {
Original file line number Diff line number Diff line change @@ -225,19 +225,24 @@ async function initConfig() {
225225
226226export async function getConfig ( ) : Promise < AdminConfig > {
227227 const storageType = process . env . NEXT_PUBLIC_STORAGE_TYPE || 'localstorage' ;
228+
228229 if ( process . env . DOCKER_ENV === 'true' || storageType === 'localstorage' ) {
229230 await initConfig ( ) ;
230231 return cachedConfig ;
231232 }
233+
232234 // 非 docker 环境且 DB 存储,直接读 db 配置
233- const storage = getStorage ( ) ;
234- let adminConfig : AdminConfig | null = null ;
235- if ( storage && typeof ( storage as any ) . getAdminConfig === 'function' ) {
236- adminConfig = await ( storage as any ) . getAdminConfig ( ) ;
237- }
238- if ( adminConfig ) {
239- // 合并一些环境变量配置
240- adminConfig . SiteConfig . SiteName = process . env . SITE_NAME || 'KatelyaTV' ;
235+ try {
236+ const storage = getStorage ( ) ;
237+ let adminConfig : AdminConfig | null = null ;
238+
239+ if ( storage && typeof ( storage as any ) . getAdminConfig === 'function' ) {
240+ adminConfig = await ( storage as any ) . getAdminConfig ( ) ;
241+ }
242+
243+ if ( adminConfig ) {
244+ // 合并一些环境变量配置
245+ adminConfig . SiteConfig . SiteName = process . env . SITE_NAME || 'KatelyaTV' ;
241246 adminConfig . SiteConfig . Announcement =
242247 process . env . ANNOUNCEMENT ||
243248 '本网站仅提供影视信息搜索服务,所有内容均来自第三方网站。本站不存储任何视频资源,不对任何内容的准确性、合法性、完整性负责。' ;
@@ -306,6 +311,11 @@ export async function getConfig(): Promise<AdminConfig> {
306311 await initConfig ( ) ;
307312 }
308313 return cachedConfig ;
314+ } catch ( error ) {
315+ // 如果数据库访问失败,回退到默认配置
316+ await initConfig ( ) ;
317+ return cachedConfig ;
318+ }
309319}
310320
311321export async function resetConfig ( ) {
Original file line number Diff line number Diff line change @@ -39,7 +39,22 @@ interface D1ExecResult {
3939
4040// 获取全局D1数据库实例
4141function getD1Database ( ) : D1Database {
42- return ( process . env as any ) . DB as D1Database ;
42+ // 在 Cloudflare Pages/Workers 环境中,DB 是全局变量
43+ if ( typeof globalThis !== 'undefined' && ( globalThis as any ) . DB ) {
44+ return ( globalThis as any ) . DB as D1Database ;
45+ }
46+
47+ // 回退到 process.env(用于本地开发)
48+ if ( ( process . env as any ) . DB ) {
49+ return ( process . env as any ) . DB as D1Database ;
50+ }
51+
52+ // 最后尝试从 globalThis.process.env
53+ if ( typeof globalThis !== 'undefined' && ( globalThis as any ) . process ?. env ?. DB ) {
54+ return ( globalThis as any ) . process . env . DB as D1Database ;
55+ }
56+
57+ throw new Error ( 'D1 database not available. Make sure DB binding is configured.' ) ;
4358}
4459
4560export class D1Storage implements IStorage {
You can’t perform that action at this time.
0 commit comments