Skip to content

Commit 9483842

Browse files
authored
fix: 修复cloudflare部署失败的问题 (huangxd-#92)
* 修复cf部署报错的问题 * 增加.cache ignore
1 parent 5fa871d commit 9483842

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
package-lock.json
33
node_modules
44
.env
5+
.cache

danmu_api/utils/cache-util.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { globals } from '../configs/globals.js';
22
import { log } from './log-util.js'
33
import { Anime } from "../models/dandan-model.js";
44
import { simpleHash } from "./codec-util.js";
5-
import fs from 'fs';
6-
import path from 'path';
5+
let fs, path;
76

87
// =====================
98
// cache数据结构处理函数
@@ -409,13 +408,23 @@ export async function updateLocalCaches() {
409408
}
410409

411410
// 判断是否有效的本地缓存目录
412-
export function judgeLocalCacheValid(urlPath) {
413-
if (!globals.localCacheValid && urlPath !== "/favicon.ico" && urlPath !== "/robots.txt") {
414-
const cacheDirPath = path.join(getDirname(), '..', '..', '.cache');
411+
export async function judgeLocalCacheValid(urlPath, deployPlatform) {
412+
if (deployPlatform === 'node') {
413+
try {
414+
fs = await import('fs');
415+
path = await import('path');
415416

416-
if (fs.existsSync(cacheDirPath)) {
417-
globals.localCacheValid = true;
418-
} else {
417+
if (!globals.localCacheValid && urlPath !== "/favicon.ico" && urlPath !== "/robots.txt") {
418+
const cacheDirPath = path.join(getDirname(), '..', '..', '.cache');
419+
420+
if (fs.existsSync(cacheDirPath)) {
421+
globals.localCacheValid = true;
422+
} else {
423+
globals.localCacheValid = false;
424+
}
425+
}
426+
} catch (error) {
427+
console.warn('Node.js modules not available:', error.message);
419428
globals.localCacheValid = false;
420429
}
421430
}

danmu_api/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function handleRequest(req, env, deployPlatform, clientIp) {
1717
const method = req.method;
1818

1919
if (deployPlatform === "node") {
20-
judgeLocalCacheValid(path);
20+
await judgeLocalCacheValid(path, deployPlatform);
2121
}
2222
await judgeRedisValid(path);
2323

0 commit comments

Comments
 (0)