forked from seasalt-haiyan/OxygenBlogPlatform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext.config.ts
More file actions
190 lines (174 loc) · 5.92 KB
/
next.config.ts
File metadata and controls
190 lines (174 loc) · 5.92 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* 判断是否为静态导出模式
* 规则:
* 1. 开发环境 (NODE_ENV=development) 永远不是静态导出模式
* 2. 只有显式设置 STATIC_EXPORT=true 或 NEXT_PRIVATE_STATIC_EXPORT=true 时才启用静态导出
* 3. 生产环境默认不启用静态导出,除非显式设置
*/
const isStaticExport = process.env.NODE_ENV !== 'development' &&
(String(process.env.STATIC_EXPORT).toLowerCase() === 'true' ||
String(process.env.NEXT_PRIVATE_STATIC_EXPORT).toLowerCase() === 'true');
/**
* 判断是否为开发环境
*/
const isDev = process.env.NODE_ENV === 'development';
const repoName = process.env.NEXT_PUBLIC_GITHUB_REPO_NAME || 'OxygenBlogPlatform';
// 基础配置
const baseConfig = {
reactStrictMode: true,
// 允许的开发环境来源 - 解决跨域问题
allowedDevOrigins: ['100.143.40.229', 'localhost'],
// 静态导出模式下使用 webpack,以确保别名配置生效
// 开发模式使用 Turbopack(Next.js 16 默认)
turbopack: isStaticExport ? undefined : {},
compiler: {
reactRemoveProperties: isStaticExport,
removeConsole: isStaticExport ? { exclude: ['error'] } : false,
emotion: true,
},
// 性能优化配置
compress: true,
images: {
formats: ['image/avif', 'image/webp'],
remotePatterns: [
{
protocol: "https",
hostname: "**",
},
],
unoptimized: isStaticExport,
},
env: {
IS_STATIC_EXPORT: isStaticExport.toString(),
NEXT_PUBLIC_IS_STATIC_EXPORT: isStaticExport.toString(),
NEXT_PUBLIC_GITHUB_REPO_NAME: repoName,
},
pageExtensions: ["tsx", "ts", "jsx", "js"],
generateBuildId: async () => {
return new Date().getTime().toString();
},
poweredByHeader: false,
productionBrowserSourceMaps: !isStaticExport,
};
// 开发环境配置(支持 Server Actions)
const devConfig = {
...baseConfig,
experimental: isStaticExport ? undefined : {
serverActions: {
bodySizeLimit: '10mb',
},
},
output: undefined,
basePath: '',
assetPrefix: '',
headers: async () => {
return [
{
source: '/:path*',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable',
},
{
key: 'Strict-Transport-Security',
value: 'max-age=31536000; includeSubDomains; preload',
},
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
{
key: 'X-Frame-Options',
value: 'DENY',
},
{
key: 'X-XSS-Protection',
value: '1; mode=block',
},
{
key: 'Referrer-Policy',
value: 'strict-origin-when-cross-origin',
},
{
key: 'Permissions-Policy',
value: 'accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()',
},
{
key: 'Cross-Origin-Opener-Policy',
value: 'same-origin',
},
{
key: 'Content-Security-Policy',
value: [
"default-src 'self'",
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://giscus.app https://v6.51.la https://sdk.51.la https://www.google-analytics.com https://www.googletagmanager.com",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://giscus.app",
"img-src 'self' data: blob: https: http:",
"font-src 'self' https://fonts.gstatic.com data:",
"connect-src 'self' https://api.github.com https://giscus.app https://v6.51.la https://sdk.51.la https://www.google-analytics.com",
"media-src 'self' https: http:",
"object-src 'none'",
"frame-src https://giscus.app https://*.github.com https://player.bilibili.com",
"frame-ancestors 'self' https://*.github.io",
"form-action 'self'",
"base-uri 'self'",
"upgrade-insecure-requests",
].join('; '),
},
],
},
{
source: '/api/:path*',
headers: [
{
key: 'Cache-Control',
value: 'no-store, max-age=0',
},
],
},
];
},
};
// 静态导出配置(GitHub Pages)
const staticConfig = {
...baseConfig,
output: "export",
distDir: 'out',
trailingSlash: true,
basePath: (process.env.CUSTOM_DOMAIN === 'true' || process.env.NEXT_PUBLIC_SITE_URL === 'https://blog.xinchengp.cn') ? '' : (process.env.NEXT_PUBLIC_BASE_PATH || ''),
assetPrefix: (process.env.CUSTOM_DOMAIN === 'true' || process.env.NEXT_PUBLIC_SITE_URL === 'https://blog.xinchengp.cn') ? '' : (process.env.NEXT_PUBLIC_BASE_PATH || ''),
// 静态导出模式下禁用 Server Actions
experimental: undefined,
images: {
unoptimized: true,
remotePatterns: [
{
protocol: "https",
hostname: "**",
},
],
},
env: {
...baseConfig.env,
NEXT_PUBLIC_BASE_PATH: process.env.NEXT_PUBLIC_BASE_PATH || '',
NEXT_PUBLIC_GITHUB_REPO_NAME: process.env.NEXT_PUBLIC_GITHUB_REPO_NAME || '',
NEXT_PUBLIC_SITE_URL: process.env.NEXT_PUBLIC_SITE_URL || '',
CUSTOM_DOMAIN: process.env.CUSTOM_DOMAIN || 'false',
},
// 使用 Turbopack 别名配置替换 actions 为静态导出版本
turbopack: {
resolveAlias: {
'@/actions': './src/actions/index.static.ts',
'@/actions/todoActions': './src/actions/index.static.ts',
'@/actions/settingsActions': './src/actions/index.static.ts',
'@/actions/githubActions': './src/actions/index.static.ts',
'@/actions/backupActions': './src/actions/index.static.ts',
'@/actions/momentActions': './src/actions/index.static.ts',
'@/actions/galleryActions': './src/actions/index.static.ts',
},
},
};
// 根据环境选择配置
const nextConfig = isStaticExport ? staticConfig : devConfig;
export default nextConfig;