Skip to content

fix(media): component failing when setting a custom serverURL #213

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

Merged
merged 1 commit into from
May 15, 2025
Merged
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
4 changes: 2 additions & 2 deletions src/components/Media/ImageMedia/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { FunctionComponent } from "react";
import NextImage from "next/image";
import type { StaticImageData } from "next/image";
import { cssVariables } from "~/cssVariables";
import { getClientSideURL } from "~/utilities/get-url";
import { getMediaUrl } from "~/utilities/get-media-url";
import { cn } from "~/utilities/ui";
import type { Props as MediaProps } from "../types";

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

const cacheTag = resource.updatedAt;

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

const loading = loadingFromProps || (!priority ? "lazy" : undefined);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Media/VideoMedia/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -36,7 +36,7 @@ export const VideoMedia: FunctionComponent<MediaProps> = (props) => {
playsInline
ref={videoRef}
>
<source src={`${getClientSideURL()}/media/${filename}`} />
<source src={getMediaUrl(`/media/${filename}`)} />
</video>
);
}
Expand Down
23 changes: 23 additions & 0 deletions src/utilities/get-media-url/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getClientSideURL } from "~/utilities/get-url/";

/**
* 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}`;
};
41 changes: 41 additions & 0 deletions src/utilities/get-media-url/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from "@jest/globals";
import { getMediaUrl } from ".";

describe("utilities", () => {
describe("getMediaUrl", () => {
it("use no url", () => {
// @ts-expect-error cope with no arguments
expect(getMediaUrl()).toBe("");
});

it("use full url", () => {
expect(getMediaUrl("http://localhost:3000")).toBe(
"http://localhost:3000",
);
});

it("use full url with cacheTag", () => {
expect(getMediaUrl("http://localhost:3000", "tag=tag")).toBe(
"http://localhost:3000?tag=tag",
);
});

it("use client browser plus endpoint", () => {
Object.defineProperty(window, "location", {
value: new URL("http://localhost:3000"),
});

expect(getMediaUrl("/foo")).toBe("http://localhost:3000/foo");
});

it("use client browser plus endpoint with cacheTage", () => {
Object.defineProperty(window, "location", {
value: new URL("http://localhost:3000"),
});

expect(getMediaUrl("/foo", "tag=tag")).toBe(
"http://localhost:3000/foo?tag=tag",
);
});
});
});
Loading