jingrow/utils/siteSettings.js

27 lines
724 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// utils for global site settings
let cache = null;
let lastFetch = 0;
const CACHE_TTL = 1000 * 60 * 15; // 15分钟缓存
/**
* 获取全局站点设置自动缓存支持SSR/CSR
* @param {string} [siteUrl] SSR时建议传入完整siteUrlCSR可省略
* @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);
const json = await res.json();
cache = json.data || {};
lastFetch = Date.now();
return cache;
} catch (e) {
return {};
}
}