Skip to content

Commit e905aba

Browse files
committed
feat(config): Enhance Next.js configuration with experimental features and improved caching headers & refs #139
1 parent 290abec commit e905aba

File tree

6 files changed

+77
-20
lines changed

6 files changed

+77
-20
lines changed

.vscode/settings.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
{
22
"typescript.tsdk": "node_modules/typescript/lib",
3-
"i18n-ally.localesPaths": ["messages", "utils/i18n"],
3+
"i18n-ally.localesPaths": [
4+
"messages",
5+
"utils/i18n"
6+
],
47
"i18n-ally.sourceLanguage": "zh-CN",
5-
"i18n-ally.keystyle": "nested"
6-
}
8+
"i18n-ally.keystyle": "nested",
9+
"editor.formatOnSave": true,
10+
"editor.defaultFormatter": "biomejs.biome",
11+
"editor.codeActionsOnSave": {
12+
"source.fixAll": "explicit",
13+
"source.organizeImports": "explicit"
14+
},
15+
"[typescriptreact]": {
16+
"editor.defaultFormatter": "biomejs.biome"
17+
}
18+
}

app/layout.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ export const viewport: Viewport = {
3939
],
4040
};
4141

42-
export default async function RootLayout({
43-
children,
44-
}: {
45-
children: React.ReactNode;
46-
}) {
47-
const locale = await getLocale();
48-
const messages = await getMessages();
42+
export default async function RootLayout({ children }: { children: React.ReactNode }) {
43+
// Parallel fetch i18n data and global config to reduce waiting time
44+
const [locale, messages, { config }] = await Promise.all([
45+
getLocale(),
46+
getMessages(),
47+
getGlobalConfig(),
48+
]);
4949

50-
const { config } = await getGlobalConfig();
5150
const { theme, googleAnalyticsId } = config;
5251

5352
return (

components/utils/swr.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ export const SWR_KEYS = {
3232
* 默认配置
3333
*/
3434
const DEFAULT_SWR_CONFIG: SWRConfiguration = {
35-
revalidateOnFocus: true,
35+
revalidateOnFocus: false, // 保留这个优化
3636
revalidateOnReconnect: true,
37-
dedupingInterval: 5000, // 防抖措施,五秒内重复请求强制缓存
38-
errorRetryCount: 3, // 错误重试次数
37+
dedupingInterval: 3000, // 稍微增加防抖时间
38+
errorRetryCount: 3, // 恢复重试次数以提高可靠性
39+
errorRetryInterval: 1000,
40+
loadingTimeout: 5000, // 增加加载超时
3941
};
4042

4143
/**

next.config.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
/** @type {import('next').NextConfig} */
22
import { readFileSync } from 'node:fs';
3-
import { join } from 'node:path';
3+
import { join, dirname } from 'node:path';
4+
import { fileURLToPath } from 'node:url';
45
import createNextIntlPlugin from 'next-intl/plugin';
56
import bundleAnalyzer from '@next/bundle-analyzer';
67

8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = dirname(__filename);
10+
711
const getImageDomains = () => {
812
try {
913
const configPath = join(process.cwd(), 'config', 'generated', 'image-domains.json');
@@ -67,6 +71,10 @@ const productionConfig = {
6771
...baseConfig,
6872
output: 'standalone',
6973

74+
experimental: {
75+
optimizePackageImports: ['lucide-react', '@heroui/react'],
76+
},
77+
7078
compiler: {
7179
removeConsole: {
7280
exclude: ['error', 'warn'],
@@ -93,6 +101,10 @@ const productionConfig = {
93101
key: 'X-XSS-Protection',
94102
value: '1; mode=block',
95103
},
104+
{
105+
key: 'Cache-Control',
106+
value: 'public, max-age=300, stale-while-revalidate=60',
107+
},
96108
],
97109
},
98110
{
@@ -104,6 +116,15 @@ const productionConfig = {
104116
},
105117
],
106118
},
119+
{
120+
source: '/_next/static/(.*)',
121+
headers: [
122+
{
123+
key: 'Cache-Control',
124+
value: 'public, max-age=31536000, immutable',
125+
},
126+
],
127+
},
107128
];
108129
},
109130
};

services/config.server.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,31 @@ export async function getPreloadData() {
159159

160160
const html = await htmlResponse.text();
161161
const $ = cheerio.load(html);
162-
const preloadScript = $('#preload-data').text();
162+
163+
// Uptime Kuma version > 1.18.4, use script#preload-data to get preload data
164+
// @see https://github.com/louislam/uptime-kuma/commit/6e07ed20816969bfd1c6c06eb518171938312782
165+
// & https://github.com/louislam/uptime-kuma/issues/2186#issuecomment-1270471470
166+
let preloadScript = $('#preload-data').text();
167+
168+
if (!preloadScript || preloadScript.trim() === '') {
169+
// Uptime Kuma version <= 1.18.4, use script:contains("window.preloadData") to get preload data
170+
const scriptWithPreloadData = $('script:contains("window.preloadData")').text();
171+
172+
if (scriptWithPreloadData) {
173+
const match = scriptWithPreloadData.match(/window\.preloadData\s*=\s*({.*});/);
174+
if (match && match[1]) {
175+
preloadScript = match[1];
176+
console.log('Successfully extracted preload data from window.preloadData');
177+
} else {
178+
console.error('Failed to extract preload data with regex. Script content:', scriptWithPreloadData.slice(0, 200));
179+
}
180+
}
181+
}
163182

164-
if (!preloadScript) {
165-
throw new ConfigError('Preload script tag not found');
183+
if (!preloadScript || preloadScript.trim() === '') {
184+
console.error('HTML response preview:', html.slice(0, 500));
185+
console.error('Available script tags:', $('script').map((i, el) => $(el).attr('id') || 'no-id').get());
186+
throw new ConfigError('Preload script tag not found or empty');
166187
}
167188

168189
try {

services/utils/common.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ export const customFetchOptions = {
44
headers: {
55
'User-Agent': `Kuma-Mieru/${packageJson.version} (https://github.com/Alice39s/kuma-mieru)`,
66
Accept: 'text/html,application/json,*/*',
7+
'Accept-Encoding': '', // bypass encoding
8+
Connection: 'keep-alive',
79
},
810
maxRetries: 3,
9-
retryDelay: 1000,
10-
timeout: 10000,
11+
retryDelay: 500,
12+
timeout: 8000,
1113
};
1214

1315
/**

0 commit comments

Comments
 (0)