Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: serve public assets on self-hosted deployments

Shae Feltze3de7f7441e2
diff --git a/apps/web/__tests__/unit/proxy-self-hosted.test.ts b/apps/web/__tests__/unit/proxy-self-hosted.test.ts
index ad20bffdb..e1d3019fe 100644
--- a/apps/web/__tests__/unit/proxy-self-hosted.test.ts
+++ b/apps/web/__tests__/unit/proxy-self-hosted.test.ts
@@ -1,10 +1,62 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";
-import { describe, expect, it } from "vitest";
+import { NextRequest } from "next/server";
+import { describe, expect, it, vi } from "vitest";
+import { proxy } from "../../proxy";
+
+vi.mock("@cap/database", () => ({
+ db: () => {
+ throw new Error("Database should not be reached on self-hosted routes");
+ },
+}));
+
+vi.mock("@cap/database/schema", () => ({ organizations: {} }));
+
+vi.mock("@cap/env", () => ({
+ buildEnv: { NEXT_PUBLIC_IS_CAP: "false" },
+ serverEnv: () => ({
+ WEB_URL: "https://cap.example.com",
+ VERCEL_URL_HOST: undefined,
+ VERCEL_BRANCH_URL_HOST: undefined,
+ VERCEL_PROJECT_PRODUCTION_URL_HOST: undefined,
+ }),
+}));
+
+const request = (path: string) =>
+ proxy(new NextRequest(`https://cap.example.com${path}`));
describe("self-hosted proxy routes", () => {
it("allows browser-based CLI authorization pages", () => {
const source = readFileSync(join(process.cwd(), "proxy.ts"), "utf8");
expect(source).toContain('path.startsWith("/cli/")');
});
+
+ it.each([
+ "/logos/browsers/google-chrome.svg",
+ "/illustrations/mask-bg.webp",
+ "/sounds/recording-start.mp3",
+ "/rive/main.riv",
+ "/fonts/Inter.woff2",
+ ])("serves the public asset %s instead of redirecting", async (path) => {
+ const response = await request(path);
+
+ expect(response.status).toBe(200);
+ expect(response.headers.get("location")).toBeNull();
+ });
+
+ it("still redirects unauthenticated page routes to /login", async () => {
+ const response = await request("/pricing");
+
+ expect(response.status).toBe(307);
+ expect(response.headers.get("location")).toBe(
+ "https://cap.example.com/login",
+ );
+ });
+
+ it("does not treat a share link as a static asset", async () => {
+ const response = await request("/s/video123");
+
+ expect(response.status).toBe(200);
+ expect(response.headers.get("location")).toBeNull();
+ });
});
diff --git a/apps/web/proxy.ts b/apps/web/proxy.ts
index d5983c84d..85d40460e 100644
--- a/apps/web/proxy.ts
+++ b/apps/web/proxy.ts
@@ -54,8 +54,12 @@ export async function proxy(request: NextRequest) {
const hostname = url.hostname;
if (buildEnv.NEXT_PUBLIC_IS_CAP !== "true") {
+ // Files under public/ have no route of their own, so without this every
+ // <img src="/logos/..."> on a self-hosted instance redirects to /login.
+ const isStaticAsset = /\.[a-z0-9]+$/i.test(path);
if (
!(
+ isStaticAsset ||
path.startsWith("/s/") ||
path.startsWith("/c/") ||
path.startsWith("/cli/") ||