Skip to content

Commit f6f4e37

Browse files
committed
fix: preserve survey script base path
1 parent 281b3dc commit f6f4e37

3 files changed

Lines changed: 53 additions & 11 deletions

File tree

packages/react-native/src/components/survey-web-view.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
View,
88
} from "react-native";
99
import { WebView, type WebViewMessageEvent } from "react-native-webview";
10+
import { getSurveyScriptUrl } from "@/components/utils/survey-script-url";
1011
import { RNConfig } from "@/lib/common/config";
1112
import { Logger } from "@/lib/common/logger";
1213
import { filterSurveys, getLanguageCode, getStyling } from "@/lib/common/utils";
@@ -382,14 +383,3 @@ const renderHtml = (
382383
</html>
383384
`;
384385
};
385-
386-
const getSurveyScriptUrl = (appUrl?: string): string => {
387-
const baseUrl = appUrl ?? "http://localhost:3000";
388-
const url = new URL("/js/surveys.umd.cjs", baseUrl);
389-
390-
if (url.protocol !== "http:" && url.protocol !== "https:") {
391-
throw new Error("Formbricks appUrl must use http or https");
392-
}
393-
394-
return url.toString();
395-
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, test } from "vitest";
2+
import { getSurveyScriptUrl } from "@/components/utils/survey-script-url";
3+
4+
describe("getSurveyScriptUrl()", () => {
5+
test("builds the default local development script URL", () => {
6+
expect(getSurveyScriptUrl()).toBe("http://localhost:3000/js/surveys.umd.cjs");
7+
});
8+
9+
test("builds the script URL from a root-hosted appUrl", () => {
10+
expect(getSurveyScriptUrl("https://app.formbricks.com")).toBe(
11+
"https://app.formbricks.com/js/surveys.umd.cjs",
12+
);
13+
});
14+
15+
test("preserves a path-based deployment without a trailing slash", () => {
16+
expect(getSurveyScriptUrl("https://host/formbricks")).toBe(
17+
"https://host/formbricks/js/surveys.umd.cjs",
18+
);
19+
});
20+
21+
test("preserves a path-based deployment with a trailing slash", () => {
22+
expect(getSurveyScriptUrl("https://host/formbricks/")).toBe(
23+
"https://host/formbricks/js/surveys.umd.cjs",
24+
);
25+
});
26+
27+
test("drops query and hash components from the script URL", () => {
28+
expect(getSurveyScriptUrl("https://host/formbricks?foo=bar#section")).toBe(
29+
"https://host/formbricks/js/surveys.umd.cjs",
30+
);
31+
});
32+
33+
test("rejects non-http protocols", () => {
34+
expect(() => getSurveyScriptUrl("file:///tmp/formbricks")).toThrow(
35+
"Formbricks appUrl must use http or https",
36+
);
37+
});
38+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const getSurveyScriptUrl = (appUrl?: string): string => {
2+
const url = new URL(appUrl ?? "http://localhost:3000");
3+
4+
if (url.protocol !== "http:" && url.protocol !== "https:") {
5+
throw new Error("Formbricks appUrl must use http or https");
6+
}
7+
8+
const basePath = url.pathname.endsWith("/") ? url.pathname : `${url.pathname}/`;
9+
url.pathname = `${basePath}js/surveys.umd.cjs`;
10+
url.search = "";
11+
url.hash = "";
12+
13+
return url.toString();
14+
};

0 commit comments

Comments
 (0)