-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathnext.config.js
More file actions
281 lines (254 loc) · 7.87 KB
/
next.config.js
File metadata and controls
281 lines (254 loc) · 7.87 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @fileoverview Next.js configuration for GroqTales
* @description Enterprise-grade Next.js configuration with performance and security optimizations
*/
const fs = require('fs');
const path = require('path');
/**
* Read the canonical app version at build time.
*
* Priority order:
* 1. Root VERSION file (single source of truth — keep this in sync with package.json)
* 2. package.json "version" field (fallback if VERSION is missing)
* 3. '0.0.0' (last-resort sentinel — a value this obvious makes misconfiguration visible)
*
* The resolved value is injected as NEXT_PUBLIC_VERSION so it is baked
* into the JS bundle at build time and available in every environment
* (Cloudflare Pages, Vercel, Docker, local) without any filesystem reads at runtime.
*/
function resolveAppVersion() {
// 1. VERSION file (preferred)
try {
const versionFilePath = path.resolve(__dirname, 'VERSION');
const raw = fs.readFileSync(versionFilePath, 'utf8').trim();
if (raw) return raw;
} catch (_) {
// VERSION file not present — fall through
}
// 2. package.json version
try {
const pkg = JSON.parse(
fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf8')
);
if (pkg.version) return pkg.version;
} catch (_) {
// Should never happen in a valid project tree
}
// 3. Sentinel fallback
return '0.0.0';
}
const APP_VERSION = resolveAppVersion();
console.log(`[next.config.js] Resolved app version: ${APP_VERSION}`);
/**
* Cloudflare Pages adapter integration.
*
* @cloudflare/next-on-pages transforms the Next.js build output so it can run
* on the Cloudflare Pages edge runtime. During local development we also call
* setupDevPlatform() so that Cloudflare bindings (KV, D1, R2, etc.) are
* available through the same API used in production.
*
* Guard: only run in development so the production build stays clean.
*/
if (process.env.NODE_ENV === 'development') {
try {
const { setupDevPlatform } = require('@cloudflare/next-on-pages/next-dev');
setupDevPlatform().catch(() => {
// Non-fatal: Cloudflare bindings simply won't be available in dev
});
} catch (_) {
// Package not installed yet — run `npm install --legacy-peer-deps` to fix.
// Local Next.js dev works perfectly without it.
}
}
const isCfBuild = process.env.NEXT_PUBLIC_BUILD_MODE === 'true';
/** @type {import('next').NextConfig} */
const nextConfig = {
// Core settings
reactStrictMode: true,
swcMinify: true,
poweredByHeader: false,
// Transpile Spline packages to fix "Super constructor null" error in production
transpilePackages: ['@splinetool/react-spline', '@splinetool/runtime'],
// Performance optimizations
compress: true,
optimizeFonts: true,
// Image optimization
images: {
unoptimized: isCfBuild, // Static export cannot use Next.js Image Optimization
remotePatterns: [
{
protocol: 'https',
hostname: 'images.unsplash.com',
},
{
protocol: 'https',
hostname: 'source.unsplash.com',
},
{
protocol: 'https',
hostname: 'via.placeholder.com',
},
{
protocol: 'https',
hostname: 'ipfs.io',
},
{
protocol: 'https',
hostname: 'gateway.pinata.cloud',
},
{
protocol: 'https',
hostname: 'api.dicebear.com',
},
{
protocol: 'https',
hostname: 'picsum.photos',
},
{
protocol: 'https',
hostname: 'ik.imagekit.io',
},
{
protocol: 'https',
hostname: 'blogger.googleusercontent.com',
},
{
protocol: 'https',
hostname: 'www.nyfa.edu',
},
{
protocol: 'https',
hostname: 'celadonbooks.com',
},
{
protocol: 'https',
hostname: 'advicewonders.wordpress.com',
},
{
protocol: 'https',
hostname: 'motivatevalmorgan.com',
},
{
protocol: 'https',
hostname: 'storage.googleapis.com',
},
{
protocol: 'https',
hostname: 'upload.wikimedia.org',
},
],
formats: ['image/webp', 'image/avif'],
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
},
// Security headers (not supported with output: 'export', use public/_headers for CF)
...(isCfBuild ? {} : {
async headers() {
return [
{
source: '/(.*)',
headers: [
{ key: 'X-Frame-Options', value: 'DENY' },
{ key: 'X-Content-Type-Options', value: 'nosniff' },
{ key: 'Referrer-Policy', value: 'origin-when-cross-origin' },
{ key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
],
},
];
},
}),
// Webpack configuration
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
// Optimize bundle splitting
if (!isServer) {
config.resolve.fallback = {
...config.resolve.fallback,
fs: false,
net: false,
tls: false,
};
}
// Fix path aliases for webpack - ensures models and other @/* imports resolve correctly
config.resolve.alias = {
...config.resolve.alias,
'@': require('path').resolve(__dirname),
'@/models': require('path').resolve(__dirname, 'models'),
'@/lib': require('path').resolve(__dirname, 'lib'),
'@/components': require('path').resolve(__dirname, 'components'),
};
// Silence warnings for specific packages
config.externals.push('pino-pretty', 'lokijs', 'encoding');
// Bundle analyzer in development
if (dev && process.env.ANALYZE === 'true') {
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'server',
openAnalyzer: true,
})
);
}
return config;
},
// Environment variables
// NEXT_PUBLIC_VERSION is resolved from the VERSION file at build time so
// the correct version is always baked into the bundle — no runtime fs reads.
env: {
NEXT_PUBLIC_VERSION: APP_VERSION,
CUSTOM_KEY: process.env.CUSTOM_KEY,
},
// Redirects and rewrites (not supported with output: 'export')
...(isCfBuild ? {} : {
async redirects() {
return [
{ source: '/home', destination: '/', permanent: true },
];
},
async rewrites() {
const envUrl = process.env.NEXT_PUBLIC_API_URL;
const apiUrl = (envUrl && envUrl.startsWith('http')) ? envUrl : 'http://localhost:3001';
const sdkEnvUrl = process.env.NEXT_PUBLIC_SDK_URL;
const sdkUrl = (sdkEnvUrl && sdkEnvUrl.startsWith('http')) ? sdkEnvUrl : 'http://localhost:3002';
return [
{ source: '/api/:path*', destination: `${apiUrl}/api/:path*` },
{ source: '/sdk/:path*', destination: `${sdkUrl}/sdk/:path*` },
];
},
}),
// Output configuration
// For Cloudflare Pages: use undefined to allow @cloudflare/next-on-pages to handle dynamic routes
// 'standalone' = self-contained Node.js server for Render
// undefined = default Next.js behavior with dynamic routes and SSR
output: isCfBuild ? undefined : (process.env.BUILD_STANDALONE === 'true' ? 'standalone' : undefined),
// Experimental features
experimental: {
optimizePackageImports: [
'lucide-react',
'framer-motion',
'date-fns',
'recharts',
],
turbo: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
},
// TypeScript configuration
typescript: {
ignoreBuildErrors: true,
},
// ESLint configuration
eslint: {
ignoreDuringBuilds: true,
dirs: ['app', 'components', 'lib', 'hooks', 'utils'],
},
// Build ID generation
generateBuildId: async () => {
return `groqtales-${Date.now()}`;
},
};
module.exports = nextConfig;