image 404 issue

This commit is contained in:
sandhiya-hepl
2026-07-20 11:44:50 +05:30
parent 8f163d4933
commit 728139b69a
14 changed files with 55 additions and 25 deletions
+20 -8
View File
@@ -1,9 +1,21 @@
/**
* Vite base, e.g. "/citpl_website/" in production and "/" in local dev.
* Runtime fallback: if an old/dev build baked base "/" but the page is
* actually served under /citpl_website/, prefer the live path prefix.
*/
export const ASSET_BASE = (import.meta.env.BASE_URL || '/').endsWith('/')
? (import.meta.env.BASE_URL || '/')
: `${import.meta.env.BASE_URL || ''}/`;
function resolveAssetBase() {
const fromEnv = import.meta.env.BASE_URL || '/';
let base = fromEnv.endsWith('/') ? fromEnv : `${fromEnv}/`;
if (typeof window !== 'undefined' && base === '/') {
const match = window.location.pathname.match(/^(\/citpl_website)(?=\/|$)/);
if (match) base = `${match[1]}/`;
}
return base;
}
export const ASSET_BASE = resolveAssetBase();
/**
* Resolve content/seed asset paths for subdirectory deploys.
@@ -14,13 +26,15 @@ export function assetUrl(path) {
if (/^(data:|blob:)/i.test(path)) return path;
const assetBase = resolveAssetBase();
const prefix = assetBase.replace(/\/$/, '');
// 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}/`)) {
if (prefix && !u.pathname.startsWith(`${prefix}/`)) {
u.pathname = `${prefix}${u.pathname}`;
return u.href;
}
@@ -31,8 +45,6 @@ export function assetUrl(path) {
}
}
const prefix = ASSET_BASE.replace(/\/$/, '');
// Already under the site base
if (prefix && (path === prefix || path.startsWith(`${prefix}/`))) {
return path;
@@ -43,7 +55,7 @@ export function assetUrl(path) {
}
if (path.startsWith('assets/') || path.startsWith('uploads/')) {
return `${ASSET_BASE}${path}`;
return `${assetBase}${path}`;
}
return path;