-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext.config.ts
More file actions
171 lines (159 loc) · 3.98 KB
/
Copy pathnext.config.ts
File metadata and controls
171 lines (159 loc) · 3.98 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
166
167
168
169
170
171
import type { NextConfig } from "next";
import path from 'path';
import { InjectManifest } from 'workbox-webpack-plugin';
import packageJson from './package.json';
const nextConfig: NextConfig = {
// 開啟 React 嚴格模式
reactStrictMode: false,
// 部署相關配置
assetPrefix: process.env.BASE_PATH || '',
basePath: process.env.BASE_PATH || '',
trailingSlash: false,
publicRuntimeConfig: {
root: process.env.BASE_PATH || '',
},
// 環境變數配置
env: {
NEXT_PUBLIC_APP_VERSION: packageJson.version,
},
serverExternalPackages: [],
webpack: (config, { dev, isServer }) => {
// 只在客戶端構建中啟用 WebAssembly
if (!isServer) {
config.experiments = {
...config.experiments,
asyncWebAssembly: true,
}
}
// 修改 WebAssembly 模組規則
config.module.rules.push({
test: /\.wasm$/,
type: "webassembly/async",
});
// 只在生產環境的客戶端構建中啟用檔案系統快取
if (!dev && !isServer) {
config.cache = {
type: 'filesystem',
buildDependencies: {
config: [__filename],
},
cacheDirectory: path.resolve(__dirname, '.next/cache'),
}
}
// 處理字體檔案
config.module.rules.push({
test: /\.(ttf|woff|woff2)$/,
type: 'asset/resource',
generator: {
filename: 'static/fonts/[hash][ext]'
}
})
// 只在客戶端生產環境建置中加入 Service Worker
if (!isServer && !dev) {
config.plugins.push(
new InjectManifest({
swSrc: './public/sw.js',
swDest: './public/sw.js',
exclude: [/\.map$/, /^manifest.*\.js(?:on)?$/],
})
);
}
// 返回修改後的配置
return config;
},
// 添加 CORS 標頭
async headers() {
return [
{
source: '/api/:path*',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, s-maxage=31536000, stale-while-revalidate=86400, immutable'
},
{
key: 'CF-Cache-Control',
value: 'max-age=31536000, stale-while-revalidate=86400'
},
{
key: 'Access-Control-Allow-Origin',
value: '*',
},
{
key: 'Access-Control-Allow-Methods',
value: 'GET, POST, OPTIONS',
},
{
key: 'Access-Control-Allow-Headers',
value: 'Content-Type',
},
{
key: 'X-Content-Type-Options',
value: 'nosniff'
},
],
},
{
source: '/fonts/:path*',
headers: [
{
key: 'Access-Control-Allow-Origin',
value: '*',
},
{
key: 'Access-Control-Allow-Methods',
value: 'GET',
},
{
key: 'Access-Control-Allow-Headers',
value: 'Content-Type',
},
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
],
},
{
source: '/fonts/subsets/:path*',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
},
{
key: 'Access-Control-Allow-Origin',
value: '*',
}
],
},
{
source: '/fonts/css/:path*',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
},
{
key: 'Access-Control-Allow-Origin',
value: '*',
}
],
},
{
source: '/sw.js',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=0, must-revalidate',
},
{
key: 'Service-Worker-Allowed',
value: '/',
},
],
},
]
},
};
export default nextConfig;