-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext.config.js
More file actions
111 lines (102 loc) · 2.88 KB
/
next.config.js
File metadata and controls
111 lines (102 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/** @type {import('next').NextConfig} */
const fs = require('fs');
const path = require('path');
// 配置文件路径
const CONFIG_FILE_PATH = path.join(process.cwd(), 'freeimages.config.json');
// 获取配置
const getConfig = () => {
try {
// 检查配置文件是否存在
if (fs.existsSync(CONFIG_FILE_PATH)) {
try {
const configData = fs.readFileSync(CONFIG_FILE_PATH, 'utf-8');
return JSON.parse(configData);
} catch (parseError) {
console.error('解析配置文件失败:', parseError);
}
} else {
console.log('配置文件不存在,使用默认配置');
// 尝试创建默认配置文件
try {
const defaultConfig = {
app: {
site: {
url: process.env.APP_URL || 'http://localhost:3000',
},
images: {
domains: [],
formats: ['image/avif', 'image/webp'],
}
},
storage: {
cloudflare: {
publicDomain: '',
}
}
};
const configDir = path.dirname(CONFIG_FILE_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
fs.writeFileSync(CONFIG_FILE_PATH, JSON.stringify(defaultConfig, null, 2), 'utf-8');
console.log('已创建默认配置文件');
return defaultConfig;
} catch (createError) {
console.error('创建默认配置文件失败:', createError);
}
}
} catch (error) {
console.error('读取配置文件失败:', error);
}
// 如果所有尝试都失败,返回默认配置
return {
app: {
site: {
url: process.env.APP_URL || 'http://localhost:3000',
},
images: {
domains: [],
formats: ['image/avif', 'image/webp'],
}
},
storage: {
cloudflare: {
publicDomain: '',
}
}
};
};
// 获取图片域名
const getImageDomains = (config) => {
try {
const domains = [...(config.app.images.domains || [])];
// 添加R2域名(如果存在)
if (config.storage.cloudflare.publicDomain) {
// 移除可能的协议前缀
const domain = config.storage.cloudflare.publicDomain.replace(/^https?:\/\//, '');
if (!domains.includes(domain)) {
domains.push(domain);
}
}
return domains;
} catch (error) {
console.error('获取图片域名失败:', error);
return [];
}
};
// 获取配置
const config = getConfig();
const imageDomains = getImageDomains(config);
const imageFormats = config.app?.images?.formats || ['image/avif', 'image/webp'];
const nextConfig = {
reactStrictMode: true,
images: {
domains: imageDomains,
formats: imageFormats,
},
// 环境变量
env: {
APP_URL: config.app?.site?.url || process.env.APP_URL || 'http://localhost:3000',
},
}
module.exports = nextConfig