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
5 changes: 5 additions & 0 deletions .changeset/fluffy-comics-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Fix double slash in FAPI client URLs when using a proxy configuration (avoids 308 redirects).
21 changes: 21 additions & 0 deletions packages/clerk-js/src/core/__tests__/fapiClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ const fapiClientWithProxy = createFapiClient({
proxyUrl,
});

const proxyUrlWithTrailingSlash = 'https://clerk.com/api/__clerk/';

const fapiClientWithProxyTrailingSlash = createFapiClient({
...baseFapiClientOptions,
proxyUrl: proxyUrlWithTrailingSlash,
});

type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
};
Expand Down Expand Up @@ -79,6 +86,20 @@ describe('buildUrl(options)', () => {
);
});

it('returns the correct URL when proxy URL has a trailing slash', () => {
// The expected URL should NOT have double slashes after __clerk
expect(fapiClientWithProxyTrailingSlash.buildUrl({ path: '/foo' }).href).toBe(
`https://clerk.com/api/__clerk/v1/foo?__clerk_api_version=${SUPPORTED_FAPI_VERSION}&_clerk_js_version=test`,
);
});

it('handles complex paths correctly with proxy URL with trailing slash', () => {
const path = '/client/sign_ins/sia_123/prepare_first_factor';
expect(fapiClientWithProxyTrailingSlash.buildUrl({ path }).href).toBe(
`https://clerk.com/api/__clerk/v1${path}?__clerk_api_version=${SUPPORTED_FAPI_VERSION}&_clerk_js_version=test`,
);
});

it('uses domain from options if production', () => {
expect(
createFapiClient({ ...baseFapiClientOptions, domain: 'clerk.other.com', instanceType: 'production' }).buildUrl({
Expand Down
5 changes: 4 additions & 1 deletion packages/clerk-js/src/core/fapiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {

if (options.proxyUrl) {
const proxyBase = new URL(options.proxyUrl);
const proxyPath = proxyBase.pathname.slice(1, proxyBase.pathname.length);
let proxyPath = proxyBase.pathname.slice(1);
if (proxyPath.endsWith('/')) {
proxyPath = proxyPath.slice(0, -1);
}
return buildUrlUtil(
{
base: proxyBase.origin,
Expand Down