27 lines
747 B
JavaScript
27 lines
747 B
JavaScript
// utils for global site settings
|
||
let cache = null;
|
||
let lastFetch = 0;
|
||
const CACHE_TTL = 1000 * 60 * 15; // 15分钟缓存
|
||
|
||
/**
|
||
* 获取全局站点设置,自动缓存,支持SSR/CSR
|
||
* @param {string} [siteUrl] SSR时建议传入完整siteUrl,CSR可省略
|
||
* @returns {Promise<Object>} 站点设置对象
|
||
*/
|
||
export async function getSiteSettings(siteUrl) {
|
||
if (cache && Date.now() - lastFetch < CACHE_TTL) {
|
||
return cache;
|
||
}
|
||
const url = siteUrl
|
||
? `${siteUrl}/api/get-site-settings`
|
||
: '/api/get-site-settings';
|
||
try {
|
||
const res = await fetch(url, { cache: 'no-store' });
|
||
const json = await res.json();
|
||
cache = json.data || {};
|
||
lastFetch = Date.now();
|
||
return cache;
|
||
} catch (e) {
|
||
return {};
|
||
}
|
||
}
|