-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathnext.config.mjs
351 lines (315 loc) · 11.1 KB
/
next.config.mjs
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// @ts-check
import { readFileSync } from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import { createSecureHeaders } from 'next-secure-headers';
import pc from 'picocolors';
import nextI18nConfig from './next-i18next.config.mjs';
import { buildEnv } from './src/config/build-env.config.mjs';
// import { getServerRuntimeEnv } from './src/config/server-runtime-env.config.mjs';
// @ts-ignore
import { PrismaPlugin } from '@prisma/nextjs-monorepo-workaround-plugin';
// validate server env
// const _serverEnv = getServerRuntimeEnv();
const workspaceRoot = path.resolve(
path.dirname(url.fileURLToPath(import.meta.url)),
'..',
'..'
);
/**
* Once supported replace by node / eslint / ts and out of experimental, replace by
* `import packageJson from './package.json' assert { type: 'json' };`
* @type {import('type-fest').PackageJson}
*/
const packageJson = JSON.parse(
readFileSync(new URL('package.json', import.meta.url)).toString('utf8')
);
const isProd = process.env.NODE_ENV === 'production';
if (!buildEnv.NEXT_BUILD_ENV_SOURCEMAPS) {
console.log(
`- ${pc.green(
'info'
)} Sourcemaps generation have been disabled through NEXT_BUILD_ENV_SOURCEMAPS`
);
}
// @link https://github.com/jagaapple/next-secure-headers
const secureHeaders = createSecureHeaders({
contentSecurityPolicy: {
directives:
buildEnv.NEXT_BUILD_ENV_CSP === true
? {
defaultSrc: "'self'",
styleSrc: [
"'self'",
"'unsafe-inline'",
'https://unpkg.com/@graphql-yoga/graphiql/dist/style.css',
'https://meet.jitsi.si',
'https://8x8.vc',
],
scriptSrc: [
"'self'",
"'unsafe-eval'",
"'unsafe-inline'",
'https://unpkg.com/@graphql-yoga/graphiql',
// 'https://meet.jit.si/external_api.js',
// 'https://8x8.vc/external_api.js',
],
frameSrc: [
"'self'",
// 'https://meet.jit.si',
// 'https://8x8.vc',
],
connectSrc: [
"'self'",
'https://vitals.vercel-insights.com',
'https://*.sentry.io',
// 'wss://ws.pusherapp.com',
// 'wss://ws-eu.pusher.com',
// 'https://sockjs.pusher.com',
// 'https://sockjs-eu.pusher.com',
],
imgSrc: ["'self'", 'https:', 'http:', 'data:'],
workerSrc: ['blob:'],
}
: {},
},
...(buildEnv.NEXT_BUILD_ENV_CSP === true &&
process.env.NODE_ENV === 'production'
? {
forceHTTPSRedirect: [
true,
{ maxAge: 60 * 60 * 24 * 4, includeSubDomains: true },
],
}
: {}),
referrerPolicy: 'same-origin',
});
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
reactStrictMode: true,
productionBrowserSourceMaps: buildEnv.NEXT_BUILD_ENV_SOURCEMAPS === true,
i18n: nextI18nConfig.i18n,
optimizeFonts: true,
// @link https://nextjs.org/docs/pages/api-reference/next-config-js/httpAgentOptions
httpAgentOptions: {
// ⚠️ keepAlive might introduce memory-leaks for long-running servers (ie: on docker)
keepAlive: true,
},
onDemandEntries: {
// period (in ms) where the server will keep pages in the buffer
maxInactiveAge: (buildEnv.NEXT_BUILD_ENV_CI ? 3600 : 25) * 1000,
},
// @link https://nextjs.org/docs/advanced-features/compiler#minification
// Sometimes buggy so enable/disable when debugging.
swcMinify: true,
compiler: {
// emotion: true,
},
// @link https://nextjs.org/docs/basic-features/image-optimization
images: {
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
minimumCacheTTL: 60,
formats: ['image/webp'],
loader: 'default',
dangerouslyAllowSVG: false,
disableStaticImages: false,
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
remotePatterns: [
{
protocol: 'https',
hostname: 'avatars.githubusercontent.com',
},
],
unoptimized: false,
},
transpilePackages: isProd
? [
'ofetch',
// i18next is build for modern browsers
// 'i18next',
// tailwind-merge contains nullish operator ?.
// 'tailwind-merge',
]
: [],
// Standalone build
// @link https://nextjs.org/docs/advanced-features/output-file-tracing#automatically-copying-traced-files-experimental
...(buildEnv.NEXT_BUILD_ENV_OUTPUT === 'standalone'
? { output: 'standalone', outputFileTracing: true }
: {}),
experimental: {
// @link https://nextjs.org/docs/advanced-features/output-file-tracing#caveats
...(buildEnv.NEXT_BUILD_ENV_OUTPUT === 'standalone'
? { outputFileTracingRoot: workspaceRoot }
: {}),
// https://github.com/vercel/turbo/issues/4832
turbo: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
// Useful in conjunction with to `output: 'standalone'` and `outputFileTracing: true`
// to keep lambdas sizes / docker images low when vercel/nft isn't able to
// drop unneeded deps for you. ie: esbuil-musl, swc-musl... when not actually needed
//
// Note that yarn 3+/4 is less impacted thanks to supportedArchitectures.
// See https://yarnpkg.com/configuration/yarnrc#supportedArchitectures and
// config example in https://github.com/belgattitude/nextjs-monorepo-example/pull/3582
// NPM/PNPM might adopt https://github.com/npm/rfcs/pull/519 in the future.
//
// Caution: use it with care because you'll have to maintain this over time.
//
// How to debug in vercel: set NEXT_DEBUG_FUNCTION_SIZE=1 in vercel env, then
// check the last lines of vercel build.
//
// Related issue: https://github.com/vercel/next.js/issues/42641
// Caution if using pnpm you might also need to consider that things are hoisted
// under node_modules/.pnpm/<something variable>. Depends on version
//
// outputFileTracingExcludes: {
// '*': [
// '**/node_modules/@swc/core-linux-x64-gnu/**/*',
// '**/node_modules/@swc/core-linux-x64-musl/**/*',
// // If you're nor relying on mdx-remote... drop this
// '**/node_modules/esbuild/linux/**/*',
// '**/node_modules/webpack/**/*',
// '**/node_modules/terser/**/*',
// // If you're not relying on sentry edge or any weird stuff... drop this too
// // https://github.com/getsentry/sentry-javascript/pull/6982
// '**/node_modules/rollup/**/*',
// ],
// },
// Prefer loading of ES Modules over CommonJS
// @link {https://nextjs.org/blog/next-11-1#es-modules-support|Blog 11.1.0}
// @link {https://github.com/vercel/next.js/discussions/27876|Discussion}
esmExternals: true,
// Experimental monorepo support
// @link {https://github.com/vercel/next.js/pull/22867|Original PR}
// @link {https://github.com/vercel/next.js/discussions/26420|Discussion}
externalDir: true,
},
typescript: {
ignoreBuildErrors: !buildEnv.NEXT_BUILD_ENV_TYPECHECK,
tsconfigPath: buildEnv.NEXT_BUILD_ENV_TSCONFIG,
},
eslint: {
ignoreDuringBuilds: !buildEnv.NEXT_BUILD_ENV_LINT,
// dirs: [`${__dirname}/src`],
},
async headers() {
return [
{
// All page routes, not the api ones
source: '/:path((?!api).*)*',
headers: [
...secureHeaders,
{ key: 'Cross-Origin-Opener-Policy', value: 'same-origin' },
{ key: 'Cross-Origin-Embedder-Policy', value: 'same-origin' },
],
},
];
},
// @link https://nextjs.org/docs/api-reference/next.config.js/rewrites
async rewrites() {
return [
/*
{
source: `/about-us`,
destination: '/about',
},
*/
];
},
webpack: (config, { webpack, isServer }) => {
if (!isServer) {
// Fixes npm packages that depend on `fs` module
// @link https://github.com/vercel/next.js/issues/36514#issuecomment-1112074589
config.resolve.fallback = { ...config.resolve.fallback, fs: false };
}
// https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking/
config.plugins.push(
new webpack.DefinePlugin({
__SENTRY_DEBUG__: buildEnv.NEXT_BUILD_ENV_SENTRY_DEBUG,
__SENTRY_TRACING__: buildEnv.NEXT_BUILD_ENV_SENTRY_TRACING,
})
);
// Nextjs with Prisma 4.11.0+ (helps standalone build in monorepos)
// https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-monorepo
if (isServer) {
config.plugins.push(new PrismaPlugin());
}
// Grab the existing rule that handles SVG imports
const fileLoaderRule = config.module.rules.find(
(/** @type {{ test: { test: (arg0: string) => any; }; }} */ rule) =>
rule.test?.test?.('.svg')
);
config.module.rules.push(
// Reapply the existing rule, but only for svg imports ending in ?url
{
...fileLoaderRule,
test: /\.svg$/i,
resourceQuery: /url/, // *.svg?url
},
// Convert all other *.svg imports to React components
{
test: /\.svg$/i,
issuer: fileLoaderRule.issuer,
resourceQuery: { not: [...fileLoaderRule.resourceQuery.not, /url/] }, // exclude if *.svg?url
use: ['@svgr/webpack'],
}
);
// Modify the file loader rule to ignore *.svg, since we have it handled now.
fileLoaderRule.exclude = /\.svg$/i;
return config;
},
env: {
APP_NAME: packageJson.name ?? 'not-in-package.json',
APP_VERSION: packageJson.version ?? 'not-in-package.json',
BUILD_TIME: new Date().toISOString(),
},
};
let config = nextConfig;
if (
buildEnv.NEXT_BUILD_ENV_SENTRY_ENABLED === true &&
process.env.SENTRY_AUTH_TOKEN !== ''
) {
try {
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
const withSentryConfig = await import('@sentry/nextjs').then(
(mod) => mod.withSentryConfig
);
// @ts-ignore cause sentry is not always following nextjs types
config = withSentryConfig(config, {
// Additional config options for the Sentry Webpack plugin. Keep in mind that
// the following options are set automatically, and overriding them is not
// recommended:
// release, url, org, project, authToken, configFile, stripPrefix,
// urlPrefix, include, ignore
// For all available options, see:
// https://github.com/getsentry/sentry-webpack-plugin#options.
// silent: isProd, // Suppresses all logs
silent: buildEnv.NEXT_BUILD_ENV_SENTRY_DEBUG === false,
});
console.log(`- ${pc.green('info')} Sentry enabled for this build`);
} catch {
console.log(`- ${pc.red('error')} Could not enable sentry, import failed`);
}
}
if (process.env.ANALYZE === 'true') {
try {
const withBundleAnalyzer = await import('@next/bundle-analyzer').then(
(mod) => mod.default
);
config = withBundleAnalyzer({
enabled: true,
})(config);
} catch {
// Do nothing, @next/bundle-analyzer is probably purged in prod or not installed
}
}
export default config;