-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext.config.ts
More file actions
151 lines (139 loc) · 4.54 KB
/
next.config.ts
File metadata and controls
151 lines (139 loc) · 4.54 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
import type { NextConfig } from "next";
import withBundleAnalyzer from "@next/bundle-analyzer";
// Only apply base path when explicitly building for GitHub Pages deployment
const isGitHubPagesDeployment = process.env.GITHUB_PAGES_DEPLOYMENT === "true";
const nextConfig: NextConfig = {
// Keep static export for build-time compilation
output:
process.env.NEXT_STATIC_EXPORT || isGitHubPagesDeployment
? "export"
: undefined,
basePath: isGitHubPagesDeployment ? "/search" : "",
assetPrefix: isGitHubPagesDeployment ? "/search" : "",
trailingSlash: false,
reactStrictMode: true, // Enable like next-atlas
// Enable compression for static files
compress: true,
// Enable font optimization and package imports optimization
experimental: {
optimizePackageImports: ["@mui/material", "@mui/icons-material"],
// Enable tree shaking for better dead code elimination
esmExternals: true,
},
eslint: {
ignoreDuringBuilds: true,
},
// Add rewrites for development to handle /search prefix
async rewrites() {
if (isGitHubPagesDeployment) return [];
return [
{
source: "/search/:path*",
destination: "/:path*",
},
];
},
images: {
unoptimized:
process.env.NEXT_STATIC_EXPORT || isGitHubPagesDeployment ? true : false,
remotePatterns: [
{
protocol: "https",
hostname: "**.ac.uk",
},
{
protocol: "http",
hostname: "**.ac.uk",
},
{
protocol: "https",
hostname: "**.edu",
},
{
protocol: "https",
hostname: "**.org",
},
{
protocol: "https",
hostname: "**.gov",
},
{
protocol: "https",
hostname: "**.org.uk",
},
{
protocol: "https",
hostname: "**.nhs.uk",
},
{
protocol: "https",
hostname: "cataloguementalhealth.ac.uk",
},
{
protocol: "https",
hostname: "www.cataloguementalhealth.ac.uk",
},
],
// Allow data URLs for client-side rendering fallbacks
dangerouslyAllowSVG: true,
contentDispositionType: "attachment",
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
},
// Skip server-side routes during static export
skipTrailingSlashRedirect: true,
skipMiddlewareUrlNormalize: true,
// Webpack optimizations like next-atlas
webpack: (config, { dev, isServer }) => {
// Use more stable chunk names to reduce build-to-build changes
if (!dev && !isServer) {
config.output.chunkFilename = "static/js/[name].[contenthash:8].js";
config.output.filename = "static/js/[name].[contenthash:8].js";
}
// Let Next.js handle bundle splitting automatically
// Custom splitChunks was creating a massive vendor chunk that hurt performance
// Remove console.log statements in production builds (client-side only)
if (!dev && !isServer) {
// Configure terser to remove console.log, console.debug, console.info
if (config.optimization && config.optimization.minimizer) {
config.optimization.minimizer.forEach((minimizer: any) => {
if (minimizer.constructor.name === "TerserPlugin") {
minimizer.options.terserOptions = {
...minimizer.options.terserOptions,
compress: {
...minimizer.options.terserOptions?.compress,
drop_console: true, // Remove all console.* calls
drop_debugger: true, // Remove debugger statements
// Additional optimizations
pure_funcs: ["console.log", "console.info", "console.debug"],
passes: 2, // Multiple passes for better optimization
},
mangle: {
// Better variable name mangling
keep_fnames: false,
},
};
}
});
}
}
// Exclude discovery-split directory from build
config.module.rules.push({
test: /\.tsx?$/,
exclude: /discovery-split/,
});
// Module resolution is handled by Next.js optimizePackageImports
// Add performance hints for both dev and production
config.performance = {
hints: "warning",
maxEntrypointSize: 500000, // 500KB warning threshold
maxAssetSize: 500000, // 500KB per asset warning threshold
};
return config;
},
};
// Export with bundle analyzer if ANALYZE env var is set
export default withBundleAnalyzer({
enabled: process.env.ANALYZE === "true",
openAnalyzer: true,
analyzerMode: "static",
})(nextConfig);