Skip to content

Commit 83e81dc

Browse files
committed
兼容适配 NextJS14.2.4
1 parent 94b6dc5 commit 83e81dc

File tree

20 files changed

+228
-178
lines changed

20 files changed

+228
-178
lines changed

blog.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ const BLOG = {
545545
process.env.ENABLE_CACHE ||
546546
process.env.npm_lifecycle_event === 'build' ||
547547
process.env.npm_lifecycle_event === 'export', // 在打包过程中默认开启缓存,开发或运行时开启此功能意义不大。
548-
isProd: process.env.VERCEL_ENV === 'production', // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) isProd: process.env.VERCEL_ENV === 'production' // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables)
548+
isProd: process.env.VERCEL_ENV === 'production' || process.env.EXPORT, // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) isProd: process.env.VERCEL_ENV === 'production' // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables)
549549
BUNDLE_ANALYZER: process.env.ANALYZE === 'true' || false, // 是否展示编译依赖内容与大小
550550
VERSION: process.env.NEXT_PUBLIC_VERSION // 版本号
551551
}

next.config.js

+86-76
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,20 @@ function scanSubdirectories(directory) {
5353
return subdirectories
5454
}
5555

56+
/**
57+
* @type {import('next').NextConfig}
58+
*/
59+
5660
const nextConfig = {
61+
output: process.env.EXPORT ? 'export' : undefined,
62+
// 多语言, 在export时禁用
63+
i18n: process.env.EXPORT
64+
? undefined
65+
: {
66+
defaultLocale: BLOG.LANG.slice(0, 2),
67+
// 支持的所有多语言,按需填写即可
68+
locales
69+
},
5770
images: {
5871
// 图片压缩
5972
formats: ['image/avif', 'image/webp'],
@@ -71,90 +84,87 @@ const nextConfig = {
7184
},
7285

7386
// 默认将feed重定向至 /public/rss/feed.xml
74-
async redirects() {
75-
return [
76-
{
77-
source: '/feed',
78-
destination: '/rss/feed.xml',
79-
permanent: true
80-
}
81-
]
82-
},
83-
// 多语言, 在export时禁用
84-
i18n:
85-
process.env.npm_lifecycle_event === 'export'
86-
? undefined
87-
: {
88-
defaultLocale: BLOG.LANG.slice(0, 2),
89-
// 支持的所有多语言,按需填写即可
90-
locales
91-
},
87+
redirects: process.env.EXPORT
88+
? undefined
89+
: async () => {
90+
return [
91+
{
92+
source: '/feed',
93+
destination: '/rss/feed.xml',
94+
permanent: true
95+
}
96+
]
97+
},
9298
// 重写url
93-
async rewrites() {
94-
// 处理多语言重定向
95-
const langsRewrites = []
96-
if (BLOG.NOTION_PAGE_ID.indexOf(',') > 0) {
97-
const siteIds = BLOG.NOTION_PAGE_ID.split(',')
98-
const langs = []
99-
for (let index = 0; index < siteIds.length; index++) {
100-
const siteId = siteIds[index]
101-
const prefix = extractLangPrefix(siteId)
102-
// 如果包含前缀 例如 zh , en 等
103-
if (prefix) {
104-
langs.push(prefix)
105-
}
106-
console.log('[Locales]', siteId)
107-
}
99+
rewrites: process.env.EXPORT
100+
? undefined
101+
: async () => {
102+
// 处理多语言重定向
103+
const langsRewrites = []
104+
if (BLOG.NOTION_PAGE_ID.indexOf(',') > 0) {
105+
const siteIds = BLOG.NOTION_PAGE_ID.split(',')
106+
const langs = []
107+
for (let index = 0; index < siteIds.length; index++) {
108+
const siteId = siteIds[index]
109+
const prefix = extractLangPrefix(siteId)
110+
// 如果包含前缀 例如 zh , en 等
111+
if (prefix) {
112+
langs.push(prefix)
113+
}
114+
console.log('[Locales]', siteId)
115+
}
108116

109-
// 映射多语言
110-
// 示例: source: '/:locale(zh|en)/:path*' ; :locale() 会将语言放入重写后的 `?locale=` 中。
111-
langsRewrites.push(
112-
{
113-
source: `/:locale(${langs.join('|')})/:path*`,
114-
destination: '/:path*'
115-
},
116-
// 匹配没有路径的情况,例如 [domain]/zh 或 [domain]/en
117-
{
118-
source: `/:locale(${langs.join('|')})`,
119-
destination: '/'
120-
},
121-
// 匹配没有路径的情况,例如 [domain]/zh/ 或 [domain]/en/
122-
{
123-
source: `/:locale(${langs.join('|')})/`,
124-
destination: '/'
117+
// 映射多语言
118+
// 示例: source: '/:locale(zh|en)/:path*' ; :locale() 会将语言放入重写后的 `?locale=` 中。
119+
langsRewrites.push(
120+
{
121+
source: `/:locale(${langs.join('|')})/:path*`,
122+
destination: '/:path*'
123+
},
124+
// 匹配没有路径的情况,例如 [domain]/zh 或 [domain]/en
125+
{
126+
source: `/:locale(${langs.join('|')})`,
127+
destination: '/'
128+
},
129+
// 匹配没有路径的情况,例如 [domain]/zh/ 或 [domain]/en/
130+
{
131+
source: `/:locale(${langs.join('|')})/`,
132+
destination: '/'
133+
}
134+
)
125135
}
126-
)
127-
}
128136

129-
return [
130-
...langsRewrites,
131-
// 伪静态重写
132-
{
133-
source: '/:path*.html',
134-
destination: '/:path*'
135-
}
136-
]
137-
},
138-
async headers() {
139-
return [
140-
{
141-
source: '/:path*{/}?',
142-
headers: [
143-
{ key: 'Access-Control-Allow-Credentials', value: 'true' },
144-
{ key: 'Access-Control-Allow-Origin', value: '*' },
137+
return [
138+
...langsRewrites,
139+
// 伪静态重写
145140
{
146-
key: 'Access-Control-Allow-Methods',
147-
value: 'GET,OPTIONS,PATCH,DELETE,POST,PUT'
148-
},
141+
source: '/:path*.html',
142+
destination: '/:path*'
143+
}
144+
]
145+
},
146+
headers: process.env.EXPORT
147+
? undefined
148+
: async () => {
149+
return [
149150
{
150-
key: 'Access-Control-Allow-Headers',
151-
value:
152-
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
151+
source: '/:path*{/}?',
152+
headers: [
153+
{ key: 'Access-Control-Allow-Credentials', value: 'true' },
154+
{ key: 'Access-Control-Allow-Origin', value: '*' },
155+
{
156+
key: 'Access-Control-Allow-Methods',
157+
value: 'GET,OPTIONS,PATCH,DELETE,POST,PUT'
158+
},
159+
{
160+
key: 'Access-Control-Allow-Headers',
161+
value:
162+
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
163+
}
164+
]
153165
}
154166
]
155-
}
156-
]
157-
},
167+
},
158168
webpack: (config, { dev, isServer }) => {
159169
// 动态主题:添加 resolve.alias 配置,将动态路径映射到实际路径
160170
if (!isServer) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"build": "next build",
1818
"start": "next start",
1919
"post-build": "next-sitemap --config next-sitemap.config.js",
20-
"export": "next build && next-sitemap --config next-sitemap.config.js && next export",
20+
"export": "cross-env EXPORT=true next build && next-sitemap --config next-sitemap.config.js",
2121
"bundle-report": "cross-env ANALYZE=true next build",
2222
"build-all-in-dev": "cross-env VERCEL_ENV=production next build"
2323
},

pages/[prefix]/[slug]/[...suffix].js

+14-10
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ export async function getStaticProps({
8383
props.post = null
8484
return {
8585
props,
86-
revalidate: siteConfig(
87-
'REVALIDATE_SECOND',
88-
BLOG.NEXT_REVALIDATE_SECOND,
89-
props.NOTION_CONFIG
90-
)
86+
revalidate: process.env.EXPORT
87+
? undefined
88+
: siteConfig(
89+
'NEXT_REVALIDATE_SECOND',
90+
BLOG.NEXT_REVALIDATE_SECOND,
91+
props.NOTION_CONFIG
92+
)
9193
}
9294
}
9395

@@ -122,11 +124,13 @@ export async function getStaticProps({
122124
delete props.allPages
123125
return {
124126
props,
125-
revalidate: siteConfig(
126-
'NEXT_REVALIDATE_SECOND',
127-
BLOG.NEXT_REVALIDATE_SECOND,
128-
props.NOTION_CONFIG
129-
)
127+
revalidate: process.env.EXPORT
128+
? undefined
129+
: siteConfig(
130+
'NEXT_REVALIDATE_SECOND',
131+
BLOG.NEXT_REVALIDATE_SECOND,
132+
props.NOTION_CONFIG
133+
)
130134
}
131135
}
132136

pages/[prefix]/[slug]/index.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
7272
props.post = null
7373
return {
7474
props,
75-
revalidate: siteConfig(
76-
'REVALIDATE_SECOND',
77-
BLOG.NEXT_REVALIDATE_SECOND,
78-
props.NOTION_CONFIG
79-
)
75+
revalidate: process.env.EXPORT
76+
? undefined
77+
: siteConfig(
78+
'NEXT_REVALIDATE_SECOND',
79+
BLOG.NEXT_REVALIDATE_SECOND,
80+
props.NOTION_CONFIG
81+
)
8082
}
8183
}
8284

@@ -111,11 +113,13 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
111113
delete props.allPages
112114
return {
113115
props,
114-
revalidate: siteConfig(
115-
'NEXT_REVALIDATE_SECOND',
116-
BLOG.NEXT_REVALIDATE_SECOND,
117-
props.NOTION_CONFIG
118-
)
116+
revalidate: process.env.EXPORT
117+
? undefined
118+
: siteConfig(
119+
'NEXT_REVALIDATE_SECOND',
120+
BLOG.NEXT_REVALIDATE_SECOND,
121+
props.NOTION_CONFIG
122+
)
119123
}
120124
}
121125

pages/[prefix]/index.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ export async function getStaticProps({ params: { prefix }, locale }) {
151151
props.post = null
152152
return {
153153
props,
154-
revalidate: siteConfig(
155-
'REVALIDATE_SECOND',
156-
BLOG.NEXT_REVALIDATE_SECOND,
157-
props.NOTION_CONFIG
158-
)
154+
revalidate: process.env.EXPORT
155+
? undefined
156+
: siteConfig(
157+
'NEXT_REVALIDATE_SECOND',
158+
BLOG.NEXT_REVALIDATE_SECOND,
159+
props.NOTION_CONFIG
160+
)
159161
}
160162
}
161163

@@ -191,11 +193,13 @@ export async function getStaticProps({ params: { prefix }, locale }) {
191193
delete props.allPages
192194
return {
193195
props,
194-
revalidate: siteConfig(
195-
'NEXT_REVALIDATE_SECOND',
196-
BLOG.NEXT_REVALIDATE_SECOND,
197-
props.NOTION_CONFIG
198-
)
196+
revalidate: process.env.EXPORT
197+
? undefined
198+
: siteConfig(
199+
'NEXT_REVALIDATE_SECOND',
200+
BLOG.NEXT_REVALIDATE_SECOND,
201+
props.NOTION_CONFIG
202+
)
199203
}
200204
}
201205

pages/archive/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ export async function getStaticProps({ locale }) {
6161

6262
return {
6363
props,
64-
revalidate: siteConfig(
65-
'NEXT_REVALIDATE_SECOND',
66-
BLOG.NEXT_REVALIDATE_SECOND,
67-
props.NOTION_CONFIG
68-
)
64+
revalidate: process.env.EXPORT
65+
? undefined
66+
: siteConfig(
67+
'NEXT_REVALIDATE_SECOND',
68+
BLOG.NEXT_REVALIDATE_SECOND,
69+
props.NOTION_CONFIG
70+
)
6971
}
7072
}
7173

pages/category/[category]/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ export async function getStaticProps({ params: { category }, locale }) {
4949

5050
return {
5151
props,
52-
revalidate: siteConfig(
53-
'NEXT_REVALIDATE_SECOND',
54-
BLOG.NEXT_REVALIDATE_SECOND,
55-
props.NOTION_CONFIG
56-
)
52+
revalidate: process.env.EXPORT
53+
? undefined
54+
: siteConfig(
55+
'NEXT_REVALIDATE_SECOND',
56+
BLOG.NEXT_REVALIDATE_SECOND,
57+
props.NOTION_CONFIG
58+
)
5759
}
5860
}
5961

pages/category/[category]/page/[page].js

+7-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ export async function getStaticProps({ params: { category, page } }) {
4444

4545
return {
4646
props,
47-
revalidate: siteConfig(
48-
'NEXT_REVALIDATE_SECOND',
49-
BLOG.NEXT_REVALIDATE_SECOND,
50-
props.NOTION_CONFIG
51-
)
47+
revalidate: process.env.EXPORT
48+
? undefined
49+
: siteConfig(
50+
'NEXT_REVALIDATE_SECOND',
51+
BLOG.NEXT_REVALIDATE_SECOND,
52+
props.NOTION_CONFIG
53+
)
5254
}
5355
}
5456

pages/category/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ export async function getStaticProps({ locale }) {
2424
delete props.allPages
2525
return {
2626
props,
27-
revalidate: siteConfig(
28-
'NEXT_REVALIDATE_SECOND',
29-
BLOG.NEXT_REVALIDATE_SECOND,
30-
props.NOTION_CONFIG
31-
)
27+
revalidate: process.env.EXPORT
28+
? undefined
29+
: siteConfig(
30+
'NEXT_REVALIDATE_SECOND',
31+
BLOG.NEXT_REVALIDATE_SECOND,
32+
props.NOTION_CONFIG
33+
)
3234
}
3335
}

0 commit comments

Comments
 (0)