-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathnext.config.js
More file actions
165 lines (158 loc) · 4.69 KB
/
next.config.js
File metadata and controls
165 lines (158 loc) · 4.69 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
const { withContentlayer } = require("next-contentlayer2")
const createNextIntlPlugin = require("next-intl/plugin")
const withNextIntl = createNextIntlPlugin("./app/i18n/request.ts")
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
})
const output = process.env.EXPORT ? "export" : undefined
const basePath = process.env.BASE_PATH || undefined
// const unoptimized = process.env.UNOPTIMIZED ? true : undefined
/**
* @type {import('next/dist/next-server/server/config').NextConfig}
**/
module.exports = () => {
const plugins = [withContentlayer, withBundleAnalyzer, withNextIntl]
const nextConfig = plugins.reduce((acc, next) => next(acc), {
async redirects() {
return [
{
source: "/:path*/feed.xml/",
destination: "/:path*/feed.xml",
permanent: true,
},
{
source: "/blog/primevue/how-to-change-css-of-primevue/",
destination: "/blog/css/how-to-change-css-of-primevue/",
permanent: true,
},
{
source: "/blog/china/china-wok-menu-guide/",
destination: "/blog/",
permanent: true,
},
]
},
output,
basePath,
trailingSlash: true,
reactStrictMode: true,
pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx"],
eslint: {
dirs: ["app", "components", "layouts", "scripts"],
},
images: {
remotePatterns: [
{
protocol: "https",
hostname: "picsum.photos",
},
{
protocol: "https",
hostname: "openweathermap.org",
},
{
protocol: "https",
hostname: "api.producthunt.com",
},
],
// 启用图片优化以减少 Fast Origin Transfer 费用
// 仅在明确需要时才禁用优化(如静态导出)
unoptimized: output === "export" ? true : false,
// 优化图片格式和缓存
formats: ["image/avif", "image/webp"],
minimumCacheTTL: 60,
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
},
webpack: (config, { isServer }) => {
config.module.rules.push({
test: /\.svg$/,
use: ["@svgr/webpack"],
})
// Optimize bundle size for Cloudflare Workers
if (isServer) {
// Exclude large client-side only dependencies from server bundle
config.externals = config.externals || []
config.externals.push({
"pdfjs-dist": "commonjs pdfjs-dist",
html2canvas: "commonjs html2canvas",
"file-saver": "commonjs file-saver",
jszip: "commonjs jszip",
heic2any: "commonjs heic2any",
jsdom: "commonjs jsdom",
})
// Optimize module resolution
config.resolve = config.resolve || {}
config.resolve.extensionAlias = {
...config.resolve.extensionAlias,
".js": [".js", ".ts", ".tsx"],
}
config.resolve.alias = {
...config.resolve.alias,
// Use lighter alternatives where possible
}
}
return config
},
// Optimize font loading
optimizeFonts: true,
// Allow font optimization to fail gracefully
experimental: {
optimizePackageImports: ["lucide-react", "react-icons", "@headlessui/react", "date-fns"],
},
// Compress output
compress: true,
// Optimize production builds
swcMinify: true,
// 忽略 TypeScript 构建错误(用于第三方库的类型问题)
typescript: {
ignoreBuildErrors: true,
},
// 添加缓存头以减少 Fast Origin Transfer 费用
async headers() {
return [
{
// 静态资源长期缓存
source: "/static/:path*",
headers: [
{
key: "Cache-Control",
value: "public, max-age=31536000, immutable",
},
],
},
{
// 字体文件长期缓存
source: "/fonts/:path*",
headers: [
{
key: "Cache-Control",
value: "public, max-age=31536000, immutable",
},
],
},
{
// 图片资源缓存
source: "/_next/image",
headers: [
{
key: "Cache-Control",
value: "public, max-age=31536000, immutable",
},
],
},
{
// Next.js 静态资源
source: "/_next/static/:path*",
headers: [
{
key: "Cache-Control",
value: "public, max-age=31536000, immutable",
},
],
},
]
},
})
return nextConfig
}