Skip to content
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
3 changes: 2 additions & 1 deletion knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"src/api/**",
"src/components/ui/**",
"src/config/announcements.ts",
"src/components/shared/BetaFeatureWrapper/BetaFeatureWrapper.tsx"
"src/components/shared/BetaFeatureWrapper/BetaFeatureWrapper.tsx",
"src/utils/publicAsset.ts"
],
"ignoreDependencies": [
"@tanstack/react-query-devtools",
Expand Down
48 changes: 48 additions & 0 deletions src/utils/publicAsset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";

import { computeAssetsBase } from "./publicAsset";

describe("computeAssetsBase", () => {
it("returns the dev server root in dev mode", () => {
const base = computeAssetsBase(
"http://localhost:5173/src/utils/publicAsset.ts",
true,
);
expect(base).toBe("http://localhost:5173/");
expect(new URL("example-pipelines/foo.yaml", base).href).toBe(
"http://localhost:5173/example-pipelines/foo.yaml",
);
});

it("returns the bundle base in built mode when assets live under a nested path", () => {
const base = computeAssetsBase(
"https://cdn.example.com/app/build-123/abc1234/assets/index-hash.js",
false,
);
expect(base).toBe("https://cdn.example.com/app/build-123/abc1234/");
expect(new URL("example-pipelines/foo.yaml", base).href).toBe(
"https://cdn.example.com/app/build-123/abc1234/example-pipelines/foo.yaml",
);
});

it("returns the same-origin base in built mode with a same-origin import.meta.url", () => {
const base = computeAssetsBase(
"https://app.example.com/assets/index-hash.js",
false,
);
expect(base).toBe("https://app.example.com/");
expect(new URL("example-pipelines/foo.yaml", base).href).toBe(
"https://app.example.com/example-pipelines/foo.yaml",
);
});

it("preserves filenames with spaces when resolved against the computed base", () => {
const base = computeAssetsBase(
"https://cdn.example.com/app/b/abc/assets/index.js",
false,
);
expect(new URL("example-pipelines/Intro-Hello World.yaml", base).href).toBe(
"https://cdn.example.com/app/b/abc/example-pipelines/Intro-Hello%20World.yaml",
);
});
});
13 changes: 13 additions & 0 deletions src/utils/publicAsset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function computeAssetsBase(metaUrl: string, isDev: boolean): string {
const here = new URL(metaUrl);
return isDev ? new URL("/", here).href : new URL("..", here).href;
}

export const ASSETS_BASE = computeAssetsBase(
import.meta.url,
Boolean(import.meta.env.DEV),
);

export function publicAsset(path: string): string {
return new URL(path, ASSETS_BASE).href;
}
Loading