forked from Alice39s/kuma-mieru
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext.config.js
More file actions
198 lines (165 loc) · 4.64 KB
/
next.config.js
File metadata and controls
198 lines (165 loc) · 4.64 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
195
196
197
198
/** @type {import('next').NextConfig} */
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import createNextIntlPlugin from 'next-intl/plugin';
import bundleAnalyzer from '@next/bundle-analyzer';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const defaultProtocols = ['https', 'http'];
const normalizePatterns = (patterns) => {
const seen = new Set();
return patterns.filter((pattern) => {
if (!pattern?.hostname || !pattern?.protocol) return false;
const key = `${pattern.protocol}://${pattern.hostname}`;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
};
const getImageRemotePatterns = () => {
try {
const configPath = join(process.cwd(), 'config', 'generated', 'image-domains.json');
const config = JSON.parse(readFileSync(configPath, 'utf8'));
if (Array.isArray(config?.patterns) && config.patterns.length > 0) {
const patterns = config.patterns.flatMap(({ hostname, protocols }) => {
if (!hostname) return [];
const resolvedProtocols = Array.isArray(protocols) && protocols.length > 0 ? protocols : defaultProtocols;
return resolvedProtocols.map((protocol) => ({ hostname, protocol }));
});
const normalized = normalizePatterns(patterns);
if (normalized.length > 0) {
return normalized;
}
}
if (Array.isArray(config?.domains) && config.domains.length > 0) {
const patterns = config.domains.flatMap((hostname) =>
defaultProtocols.map((protocol) => ({ hostname, protocol })),
);
const normalized = normalizePatterns(patterns);
if (normalized.length > 0) {
return normalized;
}
}
} catch (e) {
// Fallback to defaults below
}
return normalizePatterns(defaultProtocols.map((protocol) => ({ hostname: '*', protocol })));
};
const isDevelopment = process.env.NODE_ENV === 'development';
const baseConfig = {
poweredByHeader: false,
compress: true,
reactStrictMode: true,
images: {
remotePatterns: getImageRemotePatterns(),
formats: ['image/avif', 'image/webp'],
dangerouslyAllowSVG: true,
},
webpack: (config, { isServer, dev }) => {
config.resolve.alias = {
...config.resolve.alias,
'@': join(__dirname),
};
if (!isServer && !dev) {
config.optimization.splitChunks = {
chunks: 'all',
maxSize: 244000, // 244KB chunks
};
config.externals = {
...config.externals,
'utf-8-validate': 'commonjs utf-8-validate',
bufferutil: 'commonjs bufferutil',
};
}
if (dev) {
config.cache = {
type: 'filesystem',
buildDependencies: {
config: [__filename],
},
};
}
return config;
},
};
const productionConfig = {
...baseConfig,
output: 'standalone',
experimental: {
optimizePackageImports: ['lucide-react', '@heroui/react'],
},
compiler: {
removeConsole: {
exclude: ['error', 'warn'],
},
reactRemoveProperties: true,
},
serverExternalPackages: ['sharp', 'cheerio', 'markdown-it', 'sanitize-html'],
async headers() {
return [
{
source: '/(.*)',
headers: [
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
{
key: 'X-Frame-Options',
value: 'SAMEORIGIN',
},
{
key: 'X-XSS-Protection',
value: '1; mode=block',
},
{
key: 'Cache-Control',
value: 'public, max-age=300, stale-while-revalidate=60',
},
],
},
{
source: '/api/(.*)',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=60, stale-while-revalidate=300',
},
],
},
{
source: '/_next/static/(.*)',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable',
},
],
},
];
},
};
const developmentConfig = {
...baseConfig,
compiler: {
removeConsole: false,
},
};
const withNextIntl = createNextIntlPlugin('./utils/i18n/request.ts');
const withBundleAnalyzer = bundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
openAnalyzer: true,
});
let config = isDevelopment ? developmentConfig : productionConfig;
// Cloudflare Deployment
if (process.env.CF_DEPLOYMENT) {
config = {
...config,
experimental: {
...(config.experimental ?? {}),
runtime: 'edge',
},
};
}
export default withNextIntl(withBundleAnalyzer(config));