Skip to content

Commit 375ad8d

Browse files
authored
fix(clerk-js): Remove double slash from FAPI client urls (#6706)
1 parent 6e3f53e commit 375ad8d

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.changeset/fluffy-comics-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Fix double slash in FAPI client URLs when using a proxy configuration (avoids 308 redirects).

packages/clerk-js/src/core/__tests__/fapiClient.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ const fapiClientWithProxy = createFapiClient({
2121
proxyUrl,
2222
});
2323

24+
const proxyUrlWithTrailingSlash = 'https://clerk.com/api/__clerk/';
25+
26+
const fapiClientWithProxyTrailingSlash = createFapiClient({
27+
...baseFapiClientOptions,
28+
proxyUrl: proxyUrlWithTrailingSlash,
29+
});
30+
2431
type RecursivePartial<T> = {
2532
[P in keyof T]?: RecursivePartial<T[P]>;
2633
};
@@ -79,6 +86,20 @@ describe('buildUrl(options)', () => {
7986
);
8087
});
8188

89+
it('returns the correct URL when proxy URL has a trailing slash', () => {
90+
// The expected URL should NOT have double slashes after __clerk
91+
expect(fapiClientWithProxyTrailingSlash.buildUrl({ path: '/foo' }).href).toBe(
92+
`https://clerk.com/api/__clerk/v1/foo?__clerk_api_version=${SUPPORTED_FAPI_VERSION}&_clerk_js_version=test`,
93+
);
94+
});
95+
96+
it('handles complex paths correctly with proxy URL with trailing slash', () => {
97+
const path = '/client/sign_ins/sia_123/prepare_first_factor';
98+
expect(fapiClientWithProxyTrailingSlash.buildUrl({ path }).href).toBe(
99+
`https://clerk.com/api/__clerk/v1${path}?__clerk_api_version=${SUPPORTED_FAPI_VERSION}&_clerk_js_version=test`,
100+
);
101+
});
102+
82103
it('uses domain from options if production', () => {
83104
expect(
84105
createFapiClient({ ...baseFapiClientOptions, domain: 'clerk.other.com', instanceType: 'production' }).buildUrl({

packages/clerk-js/src/core/fapiClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
149149

150150
if (options.proxyUrl) {
151151
const proxyBase = new URL(options.proxyUrl);
152-
const proxyPath = proxyBase.pathname.slice(1, proxyBase.pathname.length);
152+
let proxyPath = proxyBase.pathname.slice(1);
153+
if (proxyPath.endsWith('/')) {
154+
proxyPath = proxyPath.slice(0, -1);
155+
}
153156
return buildUrlUtil(
154157
{
155158
base: proxyBase.origin,

0 commit comments

Comments
 (0)