Repository navigation
Expand file tree
/
Copy pathnext.config.ts
More file actions
194 lines (177 loc) · 4.61 KB
/
Copy pathnext.config.ts
File metadata and controls
194 lines (177 loc) · 4.61 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
191
192
193
194
import type { NextConfig } from 'next'
import { readdir } from 'node:fs/promises'
import path from 'node:path'
const CLOUDINARY_IMAGE_EXTENSIONS = new Set([
'.avif',
'.gif',
'.jpeg',
'.jpg',
'.png',
'.svg',
'.webp'
])
const DEFAULT_CLOUDINARY_STATIC_FOLDER = 'kyswebsite/static'
async function findStaticImages(directory: string): Promise<string[]> {
const entries = await readdir(directory, { withFileTypes: true })
const nestedFiles = await Promise.all(
entries.map(async (entry) => {
const entryPath = path.join(directory, entry.name)
if (entry.isDirectory()) {
return findStaticImages(entryPath)
}
if (
entry.isFile() &&
CLOUDINARY_IMAGE_EXTENSIONS.has(path.extname(entry.name).toLowerCase())
) {
return [entryPath]
}
return []
})
)
return nestedFiles.flat()
}
function encodeCloudinaryPath(value: string) {
return value.split('/').map(encodeURIComponent).join('/')
}
const nextConfig: NextConfig = {
/* config options here */
allowedDevOrigins: ['127.0.0.1'],
// Avoid the Pages Router dev-indicator initialization race in Next.js 16.4.
devIndicators: false,
experimental: {},
typescript: {
ignoreBuildErrors: true // 忽略 TypeScript 检查
},
turbopack: {
root: process.cwd()
},
// 优化Webpack构建
webpack: (config: any, { dev }: { dev: boolean; isServer: boolean }) => {
// 生产环境优化
if (!dev) {
config.optimization = {
...config.optimization,
// 限制并发构建任务
minimize: true,
// 启用代码分割优化
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
}
// 限制CPU使用
config.parallelism = 2
}
return config
},
reactStrictMode: true,
transpilePackages: [
'@ant-design',
'antd',
'rc-util',
'rc-pagination',
'rc-picker',
'rc-tree',
'rc-table',
'rc-input'
],
i18n: {
locales: ['zh-CN', 'zh-TW', 'en'],
defaultLocale: 'zh-CN',
localeDetection: false,
domains: [
{
domain: 'kaiyuanshe.cn',
defaultLocale: 'zh-CN'
},
{
domain: 'kaisource.org',
defaultLocale: 'en'
}
]
},
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'res.cloudinary.com'
},
{
protocol: 'https',
hostname: 'avatars.githubusercontent.com'
}
],
formats: ['image/webp', 'image/avif'],
minimumCacheTTL: 60,
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384]
},
async redirects() {
if (process.env.CLOUDINARY_STATIC_ASSETS !== 'true') {
return []
}
const cloudName =
process.env.CLOUDINARY_CLOUD_NAME ||
process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
if (!cloudName) {
throw new Error(
'CLOUDINARY_STATIC_ASSETS requires CLOUDINARY_CLOUD_NAME or NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME'
)
}
const publicDirectory = path.join(process.cwd(), 'public')
const folder = (
process.env.CLOUDINARY_STATIC_FOLDER || DEFAULT_CLOUDINARY_STATIC_FOLDER
).replace(/^\/+|\/+$/g, '')
if (!folder || folder.split('/').includes('..')) {
throw new Error(
`Invalid CLOUDINARY_STATIC_FOLDER: ${process.env.CLOUDINARY_STATIC_FOLDER}`
)
}
const files = await findStaticImages(publicDirectory)
return files.map((file) => {
const relativePath = path
.relative(publicDirectory, file)
.split(path.sep)
.join('/')
if (!/^[A-Za-z0-9._/-]+$/.test(relativePath)) {
throw new Error(
`Static image paths may only contain letters, numbers, dots, dashes and underscores: ${relativePath}`
)
}
return {
source: `/${relativePath}`,
destination: `https://res.cloudinary.com/${encodeURIComponent(cloudName)}/image/upload/${encodeCloudinaryPath(`${folder}/${relativePath}`)}`,
permanent: true
}
})
},
async headers() {
return [
{
source: '/_next/image(.*)',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/img/(.*)',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=3600, must-revalidate'
}
]
}
]
}
}
export default nextConfig