Skip to content

Commit 18c0d01

Browse files
authoredMar 1, 2024··
fix(clerk-js): Show custom pages errors when sdks are in dev mode (#2802)
* fix(clerk-js): Show custom pages errors when sdks are in dev mode * fix(clerk-js): Add changeset * fix(clerk-js): Update tests * chore(clerk-js): Cleanup * chore(remix): Remove environment from Remix SDK Metadata There is no default way to grab the env in remix, so we are skipping it for now.
1 parent 395c6d7 commit 18c0d01

File tree

14 files changed

+69
-29
lines changed

14 files changed

+69
-29
lines changed
 

‎.changeset/sour-frogs-type.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'gatsby-plugin-clerk': minor
3+
'@clerk/clerk-js': minor
4+
'@clerk/clerk-sdk-node': minor
5+
'@clerk/fastify': minor
6+
'@clerk/nextjs': minor
7+
'@clerk/clerk-react': minor
8+
'@clerk/types': minor
9+
---
10+
11+
Pass environment into `sdkMetadata` in order to detect if production clerk-js is used by other sdks in dev mode. When it is log dev warning from clerk-js.

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

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export class Clerk implements ClerkInterface {
144144
public static sdkMetadata: SDKMetadata = {
145145
name: __PKG_NAME__,
146146
version: __PKG_VERSION__,
147+
environment: process.env.NODE_ENV || 'production',
147148
};
148149

149150
public client: ClientResource | undefined;

‎packages/clerk-js/src/ui/contexts/ClerkUIComponentsContext.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,13 @@ export type UserProfileContextType = UserProfileCtx & {
225225
export const useUserProfileContext = (): UserProfileContextType => {
226226
const { componentName, customPages, ...ctx } = (React.useContext(ComponentContext) || {}) as UserProfileCtx;
227227
const { queryParams } = useRouter();
228+
const clerk = useClerk();
228229

229230
if (componentName !== 'UserProfile') {
230231
throw new Error('Clerk: useUserProfileContext called outside of the mounted UserProfile component.');
231232
}
232233

233-
const pages = createUserProfileCustomPages(customPages || []);
234+
const pages = createUserProfileCustomPages(customPages || [], clerk);
234235

235236
return {
236237
...ctx,
@@ -447,12 +448,13 @@ export const useOrganizationProfileContext = (): OrganizationProfileContextType
447448
const { componentName, customPages, ...ctx } = (React.useContext(ComponentContext) || {}) as OrganizationProfileCtx;
448449
const { navigate } = useRouter();
449450
const { displayConfig } = useEnvironment();
451+
const clerk = useClerk();
450452

451453
if (componentName !== 'OrganizationProfile') {
452454
throw new Error('Clerk: useOrganizationProfileContext called outside OrganizationProfile.');
453455
}
454456

455-
const pages = createOrganizationProfileCustomPages(customPages || []);
457+
const pages = createOrganizationProfileCustomPages(customPages || [], clerk);
456458

457459
const navigateAfterLeaveOrganization = () =>
458460
navigate(ctx.afterLeaveOrganizationUrl || displayConfig.afterLeaveOrganizationUrl);

‎packages/clerk-js/src/ui/utils/__tests__/createCustomPages.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { CustomPage } from '@clerk/types';
22

3-
import { createOrganizationProfileCustomPages, createUserProfileCustomPages } from '../createCustomPages';
3+
import {
4+
createOrganizationProfileCustomPages as cOPCP,
5+
createUserProfileCustomPages as cUPCP,
6+
} from '../createCustomPages';
7+
8+
const createUserProfileCustomPages = (arr: any) => cUPCP(arr, { sdkMetadata: { environment: 'test' } } as any);
9+
const createOrganizationProfileCustomPages = (arr: any) => cOPCP(arr, { sdkMetadata: { environment: 'test' } } as any);
410

511
describe('createCustomPages', () => {
612
describe('createUserProfileCustomPages', () => {

‎packages/clerk-js/src/ui/utils/createCustomPages.tsx

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { isDevelopmentEnvironment } from '@clerk/shared';
2-
import type { CustomPage } from '@clerk/types';
1+
import type { CustomPage, LoadedClerk } from '@clerk/types';
32

43
import { isValidUrl } from '../../utils';
54
import { ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID, USER_PROFILE_NAVBAR_ROUTE_ID } from '../constants';
65
import type { NavbarRoute } from '../elements';
76
import { Organization, TickShield, User, Users } from '../icons';
87
import { localizationKeys } from '../localization';
98
import { ExternalElementMounter } from './ExternalElementMounter';
9+
import { isDevelopmentSDK } from './runtimeEnvironment';
1010

1111
export type CustomPageContent = {
1212
url: string;
@@ -47,39 +47,43 @@ type CreateCustomPagesParams = {
4747
excludedPathsFromDuplicateWarning: string[];
4848
};
4949

50-
export const createUserProfileCustomPages = (customPages: CustomPage[]) => {
51-
return createCustomPages({
52-
customPages,
53-
getDefaultRoutes: getUserProfileDefaultRoutes,
54-
setFirstPathToRoot: setFirstPathToUserProfileRoot,
55-
excludedPathsFromDuplicateWarning: [],
56-
});
50+
export const createUserProfileCustomPages = (customPages: CustomPage[], clerk: LoadedClerk) => {
51+
return createCustomPages(
52+
{
53+
customPages,
54+
getDefaultRoutes: getUserProfileDefaultRoutes,
55+
setFirstPathToRoot: setFirstPathToUserProfileRoot,
56+
excludedPathsFromDuplicateWarning: [],
57+
},
58+
clerk,
59+
);
5760
};
5861

59-
export const createOrganizationProfileCustomPages = (customPages: CustomPage[]) => {
60-
return createCustomPages({
61-
customPages,
62-
getDefaultRoutes: getOrganizationProfileDefaultRoutes,
63-
setFirstPathToRoot: setFirstPathToOrganizationProfileRoot,
64-
excludedPathsFromDuplicateWarning: [],
65-
});
62+
export const createOrganizationProfileCustomPages = (customPages: CustomPage[], clerk: LoadedClerk) => {
63+
return createCustomPages(
64+
{
65+
customPages,
66+
getDefaultRoutes: getOrganizationProfileDefaultRoutes,
67+
setFirstPathToRoot: setFirstPathToOrganizationProfileRoot,
68+
excludedPathsFromDuplicateWarning: [],
69+
},
70+
clerk,
71+
);
6672
};
6773

68-
const createCustomPages = ({
69-
customPages,
70-
getDefaultRoutes,
71-
setFirstPathToRoot,
72-
excludedPathsFromDuplicateWarning,
73-
}: CreateCustomPagesParams) => {
74+
const createCustomPages = (
75+
{ customPages, getDefaultRoutes, setFirstPathToRoot, excludedPathsFromDuplicateWarning }: CreateCustomPagesParams,
76+
clerk: LoadedClerk,
77+
) => {
7478
const { INITIAL_ROUTES, pageToRootNavbarRouteMap, validReorderItemLabels } = getDefaultRoutes();
7579

76-
if (isDevelopmentEnvironment()) {
80+
if (isDevelopmentSDK(clerk)) {
7781
checkForDuplicateUsageOfReorderingItems(customPages, validReorderItemLabels);
7882
}
7983

8084
const validCustomPages = customPages.filter(cp => {
8185
if (!isValidPageItem(cp, validReorderItemLabels)) {
82-
if (isDevelopmentEnvironment()) {
86+
if (isDevelopmentSDK(clerk)) {
8387
console.error('Clerk: Invalid custom page data: ', cp);
8488
}
8589
return false;
@@ -96,7 +100,7 @@ const createCustomPages = ({
96100

97101
const routes = setFirstPathToRoot(allRoutes);
98102

99-
if (isDevelopmentEnvironment()) {
103+
if (isDevelopmentSDK(clerk)) {
100104
warnForDuplicatePaths(routes, excludedPathsFromDuplicateWarning);
101105
}
102106

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { isDevelopmentEnvironment } from '@clerk/shared';
2+
import type { LoadedClerk } from '@clerk/types';
3+
4+
export const isDevelopmentSDK = (clerk: LoadedClerk) =>
5+
isDevelopmentEnvironment() || clerk.sdkMetadata?.environment === 'development';

‎packages/fastify/src/__snapshots__/clerkClient.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ exports[`clerk initializes clerk with constants 1`] = `
88
"apiVersion": "v1",
99
"jwtKey": "",
1010
"sdkMetadata": {
11+
"environment": "test",
1112
"name": "@clerk/fastify",
1213
"version": "0.0.0-test",
1314
},

‎packages/fastify/src/__snapshots__/constants.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ exports[`constants from environment variables 1`] = `
3535
"JWT_KEY": "CLERK_JWT_KEY",
3636
"PUBLISHABLE_KEY": "CLERK_PUBLISHABLE_KEY",
3737
"SDK_METADATA": {
38+
"environment": "test",
3839
"name": "@clerk/fastify",
3940
"version": "0.0.0-test",
4041
},

‎packages/fastify/src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const JWT_KEY = process.env.CLERK_JWT_KEY || '';
99
export const SDK_METADATA = {
1010
name: PACKAGE_NAME,
1111
version: PACKAGE_VERSION,
12+
environment: process.env.NODE_ENV,
1213
};
1314

1415
export const { Cookies, Headers } = constants;

‎packages/gatsby-plugin-clerk/src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export const TELEMETRY_DEBUG = isTruthy(process.env.GATSBY_CLERK_TELEMETRY_DEBUG
1515
export const SDK_METADATA = {
1616
name: PACKAGE_NAME,
1717
version: PACKAGE_VERSION,
18+
environment: process.env.NODE_ENV,
1819
};

‎packages/nextjs/src/server/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const SIGN_UP_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL || '';
1515
export const SDK_METADATA = {
1616
name: PACKAGE_NAME,
1717
version: PACKAGE_VERSION,
18+
environment: process.env.NODE_ENV,
1819
};
1920

2021
export const TELEMETRY_DISABLED = isTruthy(process.env.NEXT_PUBLIC_CLERK_TELEMETRY_DISABLED);

‎packages/react/src/isomorphicClerk.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ import type {
4747
} from './types';
4848
import { isConstructor, loadClerkJsScript } from './utils';
4949

50-
const SDK_METADATA = { name: PACKAGE_NAME, version: PACKAGE_VERSION };
50+
const SDK_METADATA = {
51+
name: PACKAGE_NAME,
52+
version: PACKAGE_VERSION,
53+
environment: process.env.NODE_ENV,
54+
};
5155

5256
export interface Global {
5357
Clerk?: HeadlessBrowserClerk | BrowserClerk;

‎packages/sdk-node/src/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const loadApiEnv = () => {
3535
sdkMetadata: {
3636
name: PACKAGE_NAME,
3737
version: PACKAGE_VERSION,
38+
environment: process.env.NODE_ENV,
3839
},
3940
};
4041
};

‎packages/types/src/clerk.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type InstanceType = 'production' | 'development';
2525
export type SDKMetadata = {
2626
name: string;
2727
version: string;
28+
environment?: string;
2829
};
2930

3031
export type ListenerCallback = (emission: Resources) => void;

0 commit comments

Comments
 (0)
Please sign in to comment.