asset path and iframe setting

This commit is contained in:
sandhiya-hepl
2026-07-16 10:57:25 +05:30
parent ed459d5943
commit 5b27debfa8
13 changed files with 131 additions and 67 deletions
+62
View File
@@ -0,0 +1,62 @@
/**
* Vite base, e.g. "/citpl_website/" in production and "/" in local dev.
*/
export const ASSET_BASE = (import.meta.env.BASE_URL || '/').endsWith('/')
? (import.meta.env.BASE_URL || '/')
: `${import.meta.env.BASE_URL || ''}/`;
/**
* Resolve content/seed asset paths for subdirectory deploys.
* "/assets/foo.svg" → "/citpl_website/assets/foo.svg" in prod
*/
export function assetUrl(path) {
if (!path || typeof path !== 'string') return path;
if (/^(data:|blob:)/i.test(path)) return path;
// Absolute URL wrongly pointing at domain-root /assets or /uploads
if (/^https?:\/\//i.test(path)) {
try {
const u = new URL(path);
if (u.pathname.startsWith('/assets/') || u.pathname.startsWith('/uploads/')) {
const prefix = ASSET_BASE.replace(/\/$/, '');
if (!u.pathname.startsWith(`${prefix}/`)) {
u.pathname = `${prefix}${u.pathname}`;
return u.href;
}
}
return path;
} catch {
return path;
}
}
const prefix = ASSET_BASE.replace(/\/$/, '');
// Already under the site base
if (prefix && (path === prefix || path.startsWith(`${prefix}/`))) {
return path;
}
if (path.startsWith('/assets/') || path.startsWith('/uploads/')) {
return `${prefix}${path}`;
}
if (path.startsWith('assets/') || path.startsWith('uploads/')) {
return `${ASSET_BASE}${path}`;
}
return path;
}
/** Recursively rewrite asset strings inside content JSON. */
export function prefixAssets(obj) {
if (typeof obj === 'string') return assetUrl(obj);
if (Array.isArray(obj)) return obj.map(prefixAssets);
if (obj && typeof obj === 'object') {
const out = {};
for (const k in obj) out[k] = prefixAssets(obj[k]);
return out;
}
return obj;
}