Skip to content

Commit 2115d89

Browse files
committed
update next config to optimise build
1 parent 4db606f commit 2115d89

File tree

2 files changed

+106
-7
lines changed

2 files changed

+106
-7
lines changed

lib/lazy-components.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, { lazy } from 'react';
2+
import { Spinner } from 'opub-ui';
3+
4+
// Lazy load heavy components to improve initial page load
5+
export const LazyEChartsComponent = lazy(() => import('echarts-for-react'));
6+
7+
export const LazyChartEditor = lazy(() =>
8+
import('@/app/[locale]/dashboard/[entityType]/[entitySlug]/charts/components/ChartEditor')
9+
);
10+
11+
export const LazyMapChart = lazy(() =>
12+
import('@/components/MapChart/MapChart')
13+
);
14+
15+
// Loading fallback component
16+
export const ComponentLoader = () => (
17+
<div className="flex items-center justify-center p-8">
18+
<Spinner size={20} />
19+
</div>
20+
);
21+
22+
// Higher-order component for lazy loading with consistent loading state
23+
export const withLazyLoading = (LazyComponent: React.LazyExoticComponent<React.ComponentType<any>>) => {
24+
return (props: any) => (
25+
<React.Suspense fallback={<ComponentLoader />}>
26+
<LazyComponent {...props} />
27+
</React.Suspense>
28+
);
29+
};
30+
31+
const lazyComponents = {
32+
LazyEChartsComponent,
33+
LazyChartEditor,
34+
LazyMapChart,
35+
ComponentLoader,
36+
withLazyLoading,
37+
};
38+
39+
export default lazyComponents;

next.config.mjs

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,77 @@ jiti('./env');
99

1010
// const backendUrl = new URL(process.env.NEXT_PUBLIC_BACKEND_URL);
1111

12-
const backendUrl = new URL(process.env.BACKEND_URL);
13-
const platformUrl = new URL(process.env.NEXT_PUBLIC_PLATFORM_URL); // Use NEXT_PUBLIC_ for client-side access
12+
const backendUrl = new URL(process.env.BACKEND_URL || 'http://localhost:8000');
13+
const platformUrl = new URL(process.env.NEXT_PUBLIC_PLATFORM_URL || 'http://localhost:3000');
1414

15-
const withNextIntl = createNextIntlPlugin();
15+
const withNextIntl = createNextIntlPlugin('./i18n.ts');
1616
const nextConfig = withNextIntl({
1717
transpilePackages: ['opub-ui'],
18+
19+
// Performance optimizations
20+
experimental: {
21+
optimizePackageImports: ['opub-ui', 'echarts', 'lucide-react', '@tabler/icons-react'],
22+
webpackBuildWorker: true,
23+
optimizeCss: true,
24+
},
25+
26+
// Compiler optimizations
27+
compiler: {
28+
removeConsole: process.env.NODE_ENV === 'production',
29+
},
30+
31+
// Webpack optimizations
32+
webpack: (config, { dev, isServer }) => {
33+
// Optimize for development speed
34+
if (dev) {
35+
config.watchOptions = {
36+
poll: false,
37+
ignored: ['**/node_modules', '**/.git', '**/.next'],
38+
};
39+
}
40+
41+
// Production bundle optimizations
42+
if (!dev && !isServer) {
43+
config.optimization = config.optimization || {};
44+
config.optimization.splitChunks = config.optimization.splitChunks || {};
45+
config.optimization.splitChunks.cacheGroups = {
46+
...(config.optimization.splitChunks.cacheGroups || {}),
47+
echarts: {
48+
name: 'echarts',
49+
test: /[\\/]node_modules[\\/]echarts/,
50+
chunks: 'all',
51+
priority: 20,
52+
},
53+
icons: {
54+
name: 'icons',
55+
test: /[\\/]node_modules[\\/](@tabler\/icons-react|lucide-react)/,
56+
chunks: 'all',
57+
priority: 15,
58+
},
59+
ui: {
60+
name: 'opub-ui',
61+
test: /[\\/]node_modules[\\/]opub-ui/,
62+
chunks: 'all',
63+
priority: 10,
64+
},
65+
};
66+
}
67+
68+
return config;
69+
},
70+
1871
images: {
1972
remotePatterns: [
2073
{
2174
protocol: backendUrl.protocol.slice(0, -1),
2275
hostname: backendUrl.hostname,
76+
port: backendUrl.port || '',
77+
pathname: '/**',
2378
},
2479
{
25-
protocol: platformUrl.protocol.replace(':', ''),
80+
protocol: platformUrl.protocol.slice(0, -1),
2681
hostname: platformUrl.hostname,
27-
port: platformUrl.port || '', // empty string if no port
82+
port: platformUrl.port || '',
2883
pathname: '/**',
2984
},
3085
],
@@ -47,7 +102,12 @@ export default withSentryConfig(
47102
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
48103

49104
// Upload a larger set of source maps for prettier stack traces (increases build time)
50-
widenClientFileUpload: true,
105+
widenClientFileUpload: process.env.NODE_ENV === 'production',
106+
107+
// Disable source map upload in development for speed
108+
sourcemaps: {
109+
disable: process.env.NODE_ENV === 'development',
110+
},
51111

52112
// Uncomment to route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
53113
// This can increase your server load as well as your hosting bill.
@@ -67,5 +127,5 @@ export default withSentryConfig(
67127
// https://vercel.com/docs/cron-jobs
68128
automaticVercelMonitors: true,
69129
},
70-
process.env.SENTRY_FEATURE_ENABLED
130+
process.env.SENTRY_FEATURE_ENABLED === 'true'
71131
);

0 commit comments

Comments
 (0)