Skip to content

Commit 2dca2a8

Browse files
committed
fix(media): component failing when setting a custom serverURL
* payloadcms/payload#12214
1 parent e25f1b8 commit 2dca2a8

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

src/components/Media/ImageMedia/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { FunctionComponent } from "react";
44
import NextImage from "next/image";
55
import type { StaticImageData } from "next/image";
66
import { cssVariables } from "~/cssVariables";
7-
import { getClientSideURL } from "~/utilities/get-url";
7+
import { getMediaUrl } from "~/utilities/get-media-url";
88
import { cn } from "~/utilities/ui";
99
import type { Props as MediaProps } from "../types";
1010

@@ -46,7 +46,7 @@ export const ImageMedia: FunctionComponent<MediaProps> = (props) => {
4646

4747
const cacheTag = resource.updatedAt;
4848

49-
src = `${getClientSideURL()}${url}?${cacheTag}`;
49+
src = getMediaUrl(url, cacheTag);
5050
}
5151

5252
const loading = loadingFromProps || (!priority ? "lazy" : undefined);

src/components/Media/VideoMedia/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useEffect, useRef } from "react";
44
import type { FunctionComponent } from "react";
5-
import { getClientSideURL } from "~/utilities/get-url";
5+
import { getMediaUrl } from "~/utilities/get-media-url";
66
import { cn } from "~/utilities/ui";
77
import type { Props as MediaProps } from "../types";
88

@@ -36,7 +36,7 @@ export const VideoMedia: FunctionComponent<MediaProps> = (props) => {
3636
playsInline
3737
ref={videoRef}
3838
>
39-
<source src={`${getClientSideURL()}/media/${filename}`} />
39+
<source src={getMediaUrl(`/media/${filename}`)} />
4040
</video>
4141
);
4242
}

src/utilities/get-media-url/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getClientSideURL } from "~/utilities/get-url/";
2+
3+
/**
4+
* Processes media resource URL to ensure proper formatting
5+
* @param url The original URL from the resource
6+
* @param cacheTag Optional cache tag to append to the URL
7+
* @returns Properly formatted URL with cache tag if provided
8+
*/
9+
export const getMediaUrl = (
10+
url: string | null | undefined,
11+
cacheTag?: string | null,
12+
): string => {
13+
if (!url) return "";
14+
15+
// Check if URL already has http/https protocol
16+
if (url.startsWith("http://") || url.startsWith("https://")) {
17+
return cacheTag ? `${url}?${cacheTag}` : url;
18+
}
19+
20+
// Otherwise prepend client-side URL
21+
const baseUrl = getClientSideURL();
22+
return cacheTag ? `${baseUrl}${url}?${cacheTag}` : `${baseUrl}${url}`;
23+
};

src/utilities/get-media-url/test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
import { getMediaUrl } from ".";
3+
4+
describe("utilities", () => {
5+
describe("getMediaUrl", () => {
6+
it("use no url", () => {
7+
// @ts-expect-error cope with no arguments
8+
expect(getMediaUrl()).toBe("");
9+
});
10+
11+
it("use full url", () => {
12+
expect(getMediaUrl("http://localhost:3000")).toBe(
13+
"http://localhost:3000",
14+
);
15+
});
16+
17+
it("use full url with cacheTag", () => {
18+
expect(getMediaUrl("http://localhost:3000", "tag=tag")).toBe(
19+
"http://localhost:3000?tag=tag",
20+
);
21+
});
22+
23+
it("use client browser plus endpoint", () => {
24+
Object.defineProperty(window, "location", {
25+
value: new URL("http://localhost:3000"),
26+
});
27+
28+
expect(getMediaUrl("/foo")).toBe("http://localhost:3000/foo");
29+
});
30+
31+
it("use client browser plus endpoint with cacheTage", () => {
32+
Object.defineProperty(window, "location", {
33+
value: new URL("http://localhost:3000"),
34+
});
35+
36+
expect(getMediaUrl("/foo", "tag=tag")).toBe(
37+
"http://localhost:3000/foo?tag=tag",
38+
);
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)