Skip to content

fix(templates): Media component failing when set serverURL #12214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion templates/website/src/components/Media/ImageMedia/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Props as MediaProps } from '../types'

import { cssVariables } from '@/cssVariables'
import { getClientSideURL } from '@/utilities/getURL'
import { getMediaUrl } from '@/utilities/getMediaUrl'

const { breakpoints } = cssVariables

Expand Down Expand Up @@ -44,7 +45,7 @@ export const ImageMedia: React.FC<MediaProps> = (props) => {

const cacheTag = resource.updatedAt

src = `${getClientSideURL()}${url}?${cacheTag}`
src = getMediaUrl(url, cacheTag)
}

const loading = loadingFromProps || (!priority ? 'lazy' : undefined)
Expand Down
3 changes: 2 additions & 1 deletion templates/website/src/components/Media/VideoMedia/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, { useEffect, useRef } from 'react'
import type { Props as MediaProps } from '../types'

import { getClientSideURL } from '@/utilities/getURL'
import { getMediaUrl } from '@/utilities/getMediaUrl'

export const VideoMedia: React.FC<MediaProps> = (props) => {
const { onClick, resource, videoClassName } = props
Expand Down Expand Up @@ -37,7 +38,7 @@ export const VideoMedia: React.FC<MediaProps> = (props) => {
playsInline
ref={videoRef}
>
<source src={`${getClientSideURL()}/media/${filename}`} />
<source src={getMediaUrl(`/media/${filename}`)} />
</video>
)
}
Expand Down
20 changes: 20 additions & 0 deletions templates/website/src/utilities/getMediaUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 ''

// 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}`
}
Loading