Skip to content

Commit fa3a908

Browse files
committed
Move buildRedirectUrl to utils to avoid import cycle issue
1 parent 2b13e93 commit fa3a908

File tree

9 files changed

+77
-75
lines changed

9 files changed

+77
-75
lines changed

packages/clerk-js/bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"files": [
33
{ "path": "./dist/clerk.js", "maxSize": "625KB" },
4-
{ "path": "./dist/clerk.browser.js", "maxSize": "75KB" },
4+
{ "path": "./dist/clerk.browser.js", "maxSize": "78KB" },
55
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "117KB" },
66
{ "path": "./dist/clerk.headless*.js", "maxSize": "61KB" },
77
{ "path": "./dist/ui-common*.js", "maxSize": "113KB" },

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import type {
2828
AuthenticateWithGoogleOneTapParams,
2929
AuthenticateWithMetamaskParams,
3030
AuthenticateWithOKXWalletParams,
31+
Clerk as ClerkInterface,
3132
ClerkAPIError,
3233
ClerkAuthenticateWithWeb3Params,
33-
Clerk as ClerkInterface,
3434
ClerkOptions,
3535
ClientJSONSnapshot,
3636
ClientResource,
@@ -1993,7 +1993,7 @@ export class Clerk implements ClerkInterface {
19931993
}
19941994

19951995
if (this.session?.currentTask) {
1996-
return this.navigate(params.taskUrl ?? this.buildTasksUrl());
1996+
return navigate(params.taskUrl ?? this.buildTasksUrl());
19971997
}
19981998

19991999
return navigateToSignIn();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { logger } from '@clerk/shared/logger';
22
import type { ClerkOptions, SessionTask, SetActiveParams } from '@clerk/types';
33

4-
import { buildRedirectUrl } from '@/ui/common';
54
import { buildURL } from '@/utils';
5+
import { buildRedirectUrl } from '@/utils/redirects';
66

77
/**
88
* @internal
Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { buildURL } from '../../utils/url';
2-
import type { SignInContextType, SignUpContextType, UserProfileContextType } from './../contexts';
1+
import { buildRedirectUrl } from '@/utils/redirects';
2+
3+
import type { SignInContextType, SignUpContextType, UserProfileContextType } from '../contexts';
34

45
export const SSO_CALLBACK_PATH_ROUTE = '/sso-callback';
56
export const MAGIC_LINK_VERIFY_PATH_ROUTE = '/verify';
@@ -44,58 +45,3 @@ export function buildSSOCallbackURL(
4445
endpoint: SSO_CALLBACK_PATH_ROUTE,
4546
});
4647
}
47-
48-
type AuthQueryString = string | null | undefined;
49-
50-
type BuildRedirectUrlParams = {
51-
routing: string | undefined;
52-
authQueryString: AuthQueryString;
53-
baseUrl: string;
54-
path: string | undefined;
55-
endpoint: string;
56-
};
57-
58-
export const buildRedirectUrl = ({
59-
routing,
60-
authQueryString,
61-
baseUrl,
62-
path,
63-
endpoint,
64-
}: BuildRedirectUrlParams): string => {
65-
// If a routing strategy is not provided, default to hash routing
66-
// All routing strategies know how to convert a hash-based url to their own format
67-
// Example: navigating from a hash-based to a path-based component,
68-
// the path-based component can parse and fix the URL automatically
69-
// /#/sso-callback?code=123 -> /sso-callback?code=123
70-
if (!routing || routing === 'hash') {
71-
return buildHashBasedUrl(authQueryString, endpoint);
72-
}
73-
74-
if (routing === 'path') {
75-
return buildPathBasedUrl(path || '', authQueryString, endpoint);
76-
}
77-
78-
return buildVirtualBasedUrl(baseUrl || '', authQueryString, endpoint);
79-
};
80-
81-
const buildHashBasedUrl = (authQueryString: AuthQueryString, endpoint: string): string => {
82-
// Strip hash to get the URL where we're mounted
83-
const hash = endpoint + (authQueryString ? `?${authQueryString}` : '');
84-
return buildURL({ hash }, { stringify: true });
85-
};
86-
87-
const buildPathBasedUrl = (path: string, authQueryString: AuthQueryString, endpoint: string): string => {
88-
const searchArg = authQueryString ? { search: '?' + authQueryString } : {};
89-
return buildURL(
90-
{
91-
pathname: path + endpoint,
92-
...searchArg,
93-
},
94-
{ stringify: true },
95-
);
96-
};
97-
98-
const buildVirtualBasedUrl = (base: string, authQueryString: AuthQueryString, endpoint: string): string => {
99-
const hash = endpoint + (authQueryString ? `?${authQueryString}` : '');
100-
return buildURL({ base, hash }, { stringify: true });
101-
};

packages/clerk-js/src/ui/contexts/components/SignIn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { isAbsoluteUrl } from '@clerk/shared/url';
33
import type { SessionResource } from '@clerk/types';
44
import { createContext, useContext, useMemo } from 'react';
55

6-
import { buildTaskRedirectUrl, INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '@/core/sessionTasks';
7-
86
import { SIGN_IN_INITIAL_VALUE_KEYS } from '../../../core/constants';
7+
import { buildTaskRedirectUrl, INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '../../../core/sessionTasks';
98
import { buildURL } from '../../../utils';
9+
import { buildRedirectUrl } from '../../../utils/redirects';
1010
import { RedirectUrls } from '../../../utils/redirectUrls';
11-
import { buildRedirectUrl, MAGIC_LINK_VERIFY_PATH_ROUTE, SSO_CALLBACK_PATH_ROUTE } from '../../common/redirects';
11+
import { MAGIC_LINK_VERIFY_PATH_ROUTE, SSO_CALLBACK_PATH_ROUTE } from '../../common/redirects';
1212
import { useEnvironment, useOptions } from '../../contexts';
1313
import type { ParsedQueryString } from '../../router';
1414
import { useRouter } from '../../router';

packages/clerk-js/src/ui/contexts/components/SignUp.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { isAbsoluteUrl } from '@clerk/shared/url';
33
import type { SessionResource } from '@clerk/types';
44
import { createContext, useContext, useMemo } from 'react';
55

6-
import { buildTaskRedirectUrl, INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '@/core/sessionTasks';
7-
86
import { SIGN_UP_INITIAL_VALUE_KEYS } from '../../../core/constants';
9-
import { buildURL } from '../../../utils';
7+
import { buildTaskRedirectUrl, INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '../../../core/sessionTasks';
8+
import { MAGIC_LINK_VERIFY_PATH_ROUTE, SSO_CALLBACK_PATH_ROUTE } from '../../../ui/common';
9+
import { buildRedirectUrl, buildURL } from '../../../utils';
1010
import { RedirectUrls } from '../../../utils/redirectUrls';
11-
import { buildRedirectUrl, MAGIC_LINK_VERIFY_PATH_ROUTE, SSO_CALLBACK_PATH_ROUTE } from '../../common/redirects';
1211
import { useEnvironment, useOptions } from '../../contexts';
1312
import type { ParsedQueryString } from '../../router';
1413
import { useRouter } from '../../router';

packages/clerk-js/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from './path';
2121
export * from './props';
2222
export * from './queryStateParams';
2323
export * from './querystring';
24+
export * from './redirects';
2425
export * from './runtime';
2526
export * from './url';
2627
export * from './web3';
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { buildURL } from './url';
2+
3+
type AuthQueryString = string | null | undefined;
4+
5+
type BuildRedirectUrlParams = {
6+
routing: string | undefined;
7+
authQueryString: AuthQueryString;
8+
baseUrl: string;
9+
path: string | undefined;
10+
endpoint: string;
11+
};
12+
13+
export const buildRedirectUrl = ({
14+
routing,
15+
authQueryString,
16+
baseUrl,
17+
path,
18+
endpoint,
19+
}: BuildRedirectUrlParams): string => {
20+
// If a routing strategy is not provided, default to hash routing
21+
// All routing strategies know how to convert a hash-based url to their own format
22+
// Example: navigating from a hash-based to a path-based component,
23+
// the path-based component can parse and fix the URL automatically
24+
// /#/sso-callback?code=123 -> /sso-callback?code=123
25+
if (!routing || routing === 'hash') {
26+
return buildHashBasedUrl(authQueryString, endpoint);
27+
}
28+
29+
if (routing === 'path') {
30+
return buildPathBasedUrl(path || '', authQueryString, endpoint);
31+
}
32+
33+
return buildVirtualBasedUrl(baseUrl || '', authQueryString, endpoint);
34+
};
35+
36+
const buildHashBasedUrl = (authQueryString: AuthQueryString, endpoint: string): string => {
37+
// Strip hash to get the URL where we're mounted
38+
const hash = endpoint + (authQueryString ? `?${authQueryString}` : '');
39+
return buildURL({ hash }, { stringify: true });
40+
};
41+
42+
const buildPathBasedUrl = (path: string, authQueryString: AuthQueryString, endpoint: string): string => {
43+
const searchArg = authQueryString ? { search: '?' + authQueryString } : {};
44+
return buildURL(
45+
{
46+
pathname: path + endpoint,
47+
...searchArg,
48+
},
49+
{ stringify: true },
50+
);
51+
};
52+
53+
const buildVirtualBasedUrl = (base: string, authQueryString: AuthQueryString, endpoint: string): string => {
54+
const hash = endpoint + (authQueryString ? `?${authQueryString}` : '');
55+
return buildURL({ base, hash }, { stringify: true });
56+
};
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
'use client';
22

33
export {
4-
AuthenticateWithRedirectCallback,
5-
ClerkDegraded,
6-
ClerkFailed,
74
ClerkLoaded,
85
ClerkLoading,
6+
ClerkDegraded,
7+
ClerkFailed,
8+
SignedOut,
9+
SignedIn,
910
Protect,
10-
RedirectToCreateOrganization,
11-
RedirectToOrganizationProfile,
1211
RedirectToSignIn,
1312
RedirectToSignUp,
1413
RedirectToTasks,
1514
RedirectToUserProfile,
16-
SignedIn,
17-
SignedOut,
15+
AuthenticateWithRedirectCallback,
16+
RedirectToCreateOrganization,
17+
RedirectToOrganizationProfile,
1818
} from '@clerk/clerk-react';
1919

2020
export { MultisessionAppSupport } from '@clerk/clerk-react/internal';

0 commit comments

Comments
 (0)