Skip to content

Commit 5852e11

Browse files
ericyangpanclaude
andcommitted
feat(config): enhance Next.js configuration with security and performance optimizations
Add comprehensive security headers and optimize caching strategies for better performance and security posture. Security enhancements: - Add X-DNS-Prefetch-Control, X-Frame-Options, X-Content-Type-Options - Implement Referrer-Policy and Permissions-Policy headers - Apply security headers consistently across all routes Performance optimizations: - Expand image device sizes and optimize image settings - Add aggressive caching for static assets, fonts, and images - Implement stale-while-revalidate for OG images - Configure Turbopack rules placeholder Additional improvements: - Add TypeScript and ESLint build validation - Enable SEO-friendly URL format (no trailing slashes) - Add remote image patterns configuration structure - Improve code organization with extracted security headers 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 705253e commit 5852e11

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

next.config.ts

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,57 @@ const withBundleAnalyzer = bundleAnalyzer({
1212
enabled: process.env.ANALYZE === 'true',
1313
});
1414

15+
// Security headers configuration
16+
const securityHeaders = [
17+
{
18+
key: 'X-DNS-Prefetch-Control',
19+
value: 'on'
20+
},
21+
{
22+
key: 'X-Frame-Options',
23+
value: 'SAMEORIGIN'
24+
},
25+
{
26+
key: 'X-Content-Type-Options',
27+
value: 'nosniff'
28+
},
29+
{
30+
key: 'Referrer-Policy',
31+
value: 'strict-origin-when-cross-origin'
32+
},
33+
{
34+
key: 'Permissions-Policy',
35+
value: 'camera=(), microphone=(), geolocation=()'
36+
},
37+
];
38+
1539
const nextConfig: NextConfig = {
1640
pageExtensions: ['js', 'jsx', 'md', 'mdx', 'ts', 'tsx'],
1741

1842
// Performance optimizations
1943
compress: true, // Enable gzip compression
2044
poweredByHeader: false, // Remove X-Powered-By header for security
2145

46+
// SEO optimization
47+
trailingSlash: false, // Consistent URL format
48+
2249
// Image optimization config
2350
images: {
2451
formats: ['image/avif', 'image/webp'],
25-
deviceSizes: [640, 750, 828, 1080, 1200],
26-
imageSizes: [16, 32, 48, 64, 96],
52+
deviceSizes: [640, 750, 828, 1080, 1200, 1920],
53+
imageSizes: [16, 32, 48, 64, 96, 128, 256],
2754
minimumCacheTTL: 60,
2855
dangerouslyAllowSVG: true,
2956
contentDispositionType: 'attachment',
3057
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
58+
unoptimized: false, // Ensure images are optimized
59+
remotePatterns: [
60+
// Add remote image domains here if needed
61+
// {
62+
// protocol: 'https',
63+
// hostname: 'example.com',
64+
// },
65+
],
3166
},
3267

3368
// Experimental features for better performance
@@ -38,6 +73,12 @@ const nextConfig: NextConfig = {
3873
'@mdx-js/react',
3974
'gray-matter',
4075
],
76+
// Turbopack configuration (Next.js 15+)
77+
turbo: {
78+
rules: {
79+
// Add custom turbopack rules if needed
80+
},
81+
},
4182
},
4283

4384
// Compiler optimizations
@@ -48,26 +89,27 @@ const nextConfig: NextConfig = {
4889
} : false,
4990
},
5091

92+
// TypeScript configuration
93+
typescript: {
94+
// Set to true only if you want to skip type checking during build
95+
ignoreBuildErrors: false,
96+
},
97+
98+
// ESLint configuration
99+
eslint: {
100+
// Set to true only if you want to skip linting during build
101+
ignoreDuringBuilds: false,
102+
},
103+
51104
// Headers for better caching and security
52105
async headers() {
53106
return [
107+
// Apply security headers to all routes
54108
{
55109
source: '/:path*',
56-
headers: [
57-
{
58-
key: 'X-DNS-Prefetch-Control',
59-
value: 'on'
60-
},
61-
{
62-
key: 'X-Content-Type-Options',
63-
value: 'nosniff'
64-
},
65-
{
66-
key: 'Referrer-Policy',
67-
value: 'strict-origin-when-cross-origin'
68-
},
69-
],
110+
headers: securityHeaders,
70111
},
112+
// Aggressive caching for fonts (immutable)
71113
{
72114
source: '/fonts/:path*',
73115
headers: [
@@ -77,6 +119,7 @@ const nextConfig: NextConfig = {
77119
},
78120
],
79121
},
122+
// Cache OG images for a week with stale-while-revalidate
80123
{
81124
source: '/og-images/:path*',
82125
headers: [
@@ -86,6 +129,7 @@ const nextConfig: NextConfig = {
86129
},
87130
],
88131
},
132+
// Aggressive caching for Next.js static assets
89133
{
90134
source: '/_next/static/:path*',
91135
headers: [
@@ -95,6 +139,16 @@ const nextConfig: NextConfig = {
95139
},
96140
],
97141
},
142+
// Cache images and media files
143+
{
144+
source: '/images/:path*',
145+
headers: [
146+
{
147+
key: 'Cache-Control',
148+
value: 'public, max-age=31536000, immutable',
149+
},
150+
],
151+
},
98152
];
99153
},
100154
};

0 commit comments

Comments
 (0)