Skip to content

Commit 5aab9f0

Browse files
authoredNov 28, 2023
feat(clerk-js,types): Add routerDebug option in Clerk to log navigation destination urls (#2223)
1 parent 950f4b2 commit 5aab9f0

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed
 

‎.changeset/sharp-trains-ring.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
'@clerk/types': minor
4+
---
5+
6+
Add `routerDebug` option in `Clerk.load()` to log the destination URLs when navigating

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

+31
Original file line numberDiff line numberDiff line change
@@ -516,23 +516,31 @@ describe('Clerk singleton', () => {
516516

517517
describe('.navigate(to)', () => {
518518
let sut: Clerk;
519+
let logSpy;
519520

520521
beforeEach(() => {
522+
logSpy = jest.spyOn(console, 'log');
521523
sut = new Clerk(productionPublishableKey);
522524
});
523525

526+
afterEach(() => {
527+
logSpy?.mockRestore();
528+
});
529+
524530
it('uses window location if a custom navigate is not defined', async () => {
525531
await sut.load();
526532
const toUrl = 'http://test.host/';
527533
await sut.navigate(toUrl);
528534
expect(mockHref).toHaveBeenCalledWith(toUrl);
535+
expect(logSpy).not.toBeCalled();
529536
});
530537

531538
it('uses window location if a custom navigate is defined but destination has different origin', async () => {
532539
await sut.load({ routerPush: mockNavigate });
533540
const toUrl = 'https://www.origindifferent.com/';
534541
await sut.navigate(toUrl);
535542
expect(mockHref).toHaveBeenCalledWith(toUrl);
543+
expect(logSpy).not.toBeCalled();
536544
});
537545

538546
it('wraps custom navigate method in a promise if provided and it sync', async () => {
@@ -542,6 +550,29 @@ describe('Clerk singleton', () => {
542550
expect(res.then).toBeDefined();
543551
expect(mockHref).not.toHaveBeenCalled();
544552
expect(mockNavigate).toHaveBeenCalledWith('/path#hash');
553+
expect(logSpy).not.toBeCalled();
554+
});
555+
556+
it('logs navigation external navigation when routerDebug is enabled', async () => {
557+
await sut.load({ routerDebug: true });
558+
const toUrl = 'http://test.host/';
559+
await sut.navigate(toUrl);
560+
expect(mockHref).toHaveBeenCalledWith(toUrl);
561+
562+
expect(logSpy).toBeCalledTimes(1);
563+
expect(logSpy).toBeCalledWith(`Clerk is navigating to: ${toUrl}`);
564+
});
565+
566+
it('logs navigation custom navigation when routerDebug is enabled', async () => {
567+
await sut.load({ routerPush: mockNavigate, routerDebug: true });
568+
const toUrl = 'http://test.host/path#hash';
569+
const res = sut.navigate(toUrl);
570+
expect(res.then).toBeDefined();
571+
expect(mockHref).not.toHaveBeenCalled();
572+
expect(mockNavigate).toHaveBeenCalledWith('/path#hash');
573+
574+
expect(logSpy).toBeCalledTimes(1);
575+
expect(logSpy).toBeCalledWith(`Clerk is navigating to: ${toUrl}`);
545576
});
546577
});
547578

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ export class Clerk implements ClerkInterface {
165165
#environment?: EnvironmentResource | null;
166166
//@ts-expect-error with being undefined even though it's not possible - related to issue with ts and error thrower
167167
#fapiClient: FapiClient;
168-
//@ts-expect-error with undefined even though it's not possible - related to issue with ts and error thrower
169-
#instanceType: InstanceType;
168+
#instanceType?: InstanceType;
170169
#isReady = false;
171170

172171
#listeners: Array<(emission: Resources) => void> = [];
@@ -675,6 +674,10 @@ export class Clerk implements ClerkInterface {
675674
const customNavigate =
676675
options?.replace && this.#options.routerReplace ? this.#options.routerReplace : this.#options.routerPush;
677676

677+
if (this.#options.routerDebug) {
678+
console.log(`Clerk is navigating to: ${toURL}`);
679+
}
680+
678681
if (toURL.origin !== window.location.origin || !customNavigate) {
679682
windowNavigate(toURL);
680683
return;

‎packages/types/src/clerk.retheme.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -496,17 +496,19 @@ export type BuildUrlWithAuthParams = {
496496
useQueryParam?: boolean | null;
497497
};
498498

499-
// TODO: Make sure Isomorphic Clerk navigate can work with the correct type:
500-
// (to: string) => Promise<unknown>
501499
export type CustomNavigation = (to: string, options?: NavigateOptions) => Promise<unknown> | void;
502500

503501
export type ClerkThemeOptions = DeepSnakeToCamel<DeepPartial<DisplayThemeJSON>>;
504502

505503
export interface ClerkOptions {
506504
appearance?: Appearance;
507505
localization?: LocalizationResource;
506+
/**
507+
* Navigation
508+
*/
508509
routerPush?: (to: string) => Promise<unknown> | unknown;
509510
routerReplace?: (to: string) => Promise<unknown> | unknown;
511+
routerDebug?: boolean;
510512
polling?: boolean;
511513
selectInitialSession?: (client: ClientResource) => ActiveSessionResource | null;
512514
/** Controls if ClerkJS will load with the standard browser setup using Clerk cookies */

‎packages/types/src/clerk.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -496,17 +496,20 @@ export type BuildUrlWithAuthParams = {
496496
useQueryParam?: boolean | null;
497497
};
498498

499-
// TODO: Make sure Isomorphic Clerk navigate can work with the correct type:
500-
// (to: string) => Promise<unknown>
501499
export type CustomNavigation = (to: string, options?: NavigateOptions) => Promise<unknown> | void;
502500

503501
export type ClerkThemeOptions = DeepSnakeToCamel<DeepPartial<DisplayThemeJSON>>;
504502

505503
export interface ClerkOptions {
506504
appearance?: Appearance;
507505
localization?: LocalizationResource;
506+
/**
507+
* Navigation
508+
*/
508509
routerPush?: (to: string) => Promise<unknown> | unknown;
509510
routerReplace?: (to: string) => Promise<unknown> | unknown;
511+
routerDebug?: boolean;
512+
510513
polling?: boolean;
511514
selectInitialSession?: (client: ClientResource) => ActiveSessionResource | null;
512515
/** Controls if ClerkJS will load with the standard browser setup using Clerk cookies */

0 commit comments

Comments
 (0)
Please sign in to comment.