wwwlayermeshusa/src/utilities/getMediaUrl.ts
2026-02-11 07:19:33 +01:00

25 lines
820 B
TypeScript

import { getClientSideURL } from '@/utilities/getURL'
/**
* Processes media resource URL to ensure proper formatting
* @param url The original URL from the resource
* @param cacheTag Optional cache tag to append to the URL
* @returns Properly formatted URL with cache tag if provided
*/
export const getMediaUrl = (url: string | null | undefined, cacheTag?: string | null): string => {
if (!url) return ''
if (cacheTag && cacheTag !== '') {
cacheTag = encodeURIComponent(cacheTag)
}
// Check if URL already has http/https protocol
if (url.startsWith('http://') || url.startsWith('https://')) {
return cacheTag ? `${url}?${cacheTag}` : url
}
// Otherwise prepend client-side URL
const baseUrl = getClientSideURL()
return cacheTag ? `${baseUrl}${url}?${cacheTag}` : `${baseUrl}${url}`
}