Add page data delivery and publication guards
This commit is contained in:
@@ -6,6 +6,23 @@
|
||||
return base.replace(/\/$/, '');
|
||||
}
|
||||
|
||||
function resolvePageId(options) {
|
||||
options = options || {};
|
||||
var explicit = options.pageId != null ? String(options.pageId).trim() : '';
|
||||
if (explicit && explicit !== 'PLACEHOLDER_PAGE_ID') return explicit;
|
||||
if (typeof document !== 'undefined') {
|
||||
var meta = document.querySelector('meta[name="mindspace-page-data-page-id"]');
|
||||
if (meta && meta.content) {
|
||||
var fromMeta = String(meta.content).trim();
|
||||
if (fromMeta) return fromMeta;
|
||||
}
|
||||
}
|
||||
if (global.__MINDSPACE_PAGE_DATA__ && global.__MINDSPACE_PAGE_DATA__.pageId) {
|
||||
return String(global.__MINDSPACE_PAGE_DATA__.pageId).trim();
|
||||
}
|
||||
return explicit || null;
|
||||
}
|
||||
|
||||
function buildAuthPath(apiBase, pageId) {
|
||||
return normalizeApiBase(apiBase) + '/public/pages/' + encodeURIComponent(String(pageId || '').trim()) + '/data-auth';
|
||||
}
|
||||
@@ -41,11 +58,13 @@
|
||||
|
||||
function createClient(options) {
|
||||
options = options || {};
|
||||
var pageId = options.pageId;
|
||||
var pageId = resolvePageId(options);
|
||||
var apiBase = options.apiBase || '/api';
|
||||
var fetchImpl = options.fetchImpl || global.fetch;
|
||||
var sessionToken = options.token || null;
|
||||
if (!pageId) throw new Error('pageId 不能为空');
|
||||
if (!pageId) {
|
||||
throw new Error('pageId 未配置:请先发布页面,平台会在访问时注入 __MINDSPACE_PAGE_DATA__');
|
||||
}
|
||||
if (typeof fetchImpl !== 'function') throw new Error('fetch 不可用');
|
||||
|
||||
async function request(method, path, opts) {
|
||||
@@ -111,6 +130,7 @@
|
||||
|
||||
global.MindSpacePageData = {
|
||||
createClient: createClient,
|
||||
resolvePageId: resolvePageId,
|
||||
buildAuthPath: buildAuthPath,
|
||||
buildDataPath: buildDataPath,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user