Skip to content

feat(clerk-js): Introduce debugLogger #6452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ab3cde4
feat(clerk-js): Introduce debugLogger
jacekradko Jul 31, 2025
4931533
small fixes
jacekradko Jul 31, 2025
f9a6a97
Merge branch 'main' into feat/debug-module
jacekradko Jul 31, 2025
d24c0e8
changeset
jacekradko Jul 31, 2025
0f1803d
coderabbit
jacekradko Jul 31, 2025
3ba5c80
Merge branch 'main' into feat/debug-module
jacekradko Aug 4, 2025
775fccb
fix build
jacekradko Aug 4, 2025
b6f886a
wip
jacekradko Aug 4, 2025
eaeacb8
safe initializations
jacekradko Aug 4, 2025
c3e6e52
wip
jacekradko Aug 4, 2025
bd86e19
pr feedback
jacekradko Aug 4, 2025
a7cfc1f
pr feedback
jacekradko Aug 4, 2025
806688a
Merge branch 'main' into feat/debug-module
jacekradko Aug 5, 2025
9c234a4
Merge branch 'main' into feat/debug-module
jacekradko Aug 6, 2025
496cd4c
Integrate telemetry collector
jacekradko Aug 6, 2025
aedc788
correct telemetry endpoint
jacekradko Aug 6, 2025
9828a80
tests
jacekradko Aug 6, 2025
32ce445
wip
jacekradko Aug 6, 2025
053efaf
Merge branch 'main' into feat/debug-module
jacekradko Aug 6, 2025
59b998e
fix lint and test
jacekradko Aug 6, 2025
7c311cc
wip
jacekradko Aug 6, 2025
4bcf294
Merge branch 'main' into feat/debug-module
jacekradko Aug 6, 2025
441061e
wip
jacekradko Aug 6, 2025
d79b5db
Merge branch 'main' into feat/debug-module
jacekradko Aug 7, 2025
2170de0
refactor flush methods to avoid stacking requests
jacekradko Aug 7, 2025
7604e58
remove redundant try/catch
jacekradko Aug 7, 2025
cfc5a05
consistent flush logic
jacekradko Aug 7, 2025
2f4b36b
Resolve merge conflicts: bundlewatch thresholds, TelemetryCollector l…
jacekradko Aug 8, 2025
5ff7b86
wip
jacekradko Aug 8, 2025
1562ebf
Merge branch 'main' into feat/debug-module
jacekradko Aug 11, 2025
f532cf0
wip
jacekradko Aug 11, 2025
55bda4e
Merge branch 'main' into feat/debug-module
jacekradko Aug 12, 2025
243f598
pr feedback
jacekradko Aug 12, 2025
b25d4d4
pr feedback
jacekradko Aug 12, 2025
a1994c4
wip
jacekradko Aug 12, 2025
fa0cffd
Merge branch 'main' into feat/debug-module
jacekradko Aug 12, 2025
e544597
fix lint
jacekradko Aug 12, 2025
c7798ff
Merge branch 'main' into feat/debug-module
jacekradko Aug 12, 2025
275ba94
wip
jacekradko Aug 12, 2025
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
6 changes: 6 additions & 0 deletions .changeset/all-cougars-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': patch
'@clerk/types': patch
---

Introduce debugLogger for internal debugging support
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ exports[`Typedoc output > should have a deliberate file structure 1`] = `
"types/sign-up-resource.mdx",
"types/signed-in-session-resource.mdx",
"types/state-selectors.mdx",
"types/telemetry-log-entry.mdx",
"types/use-auth-return.mdx",
"types/use-session-list-return.mdx",
"types/use-session-return.mdx",
Expand Down
10 changes: 5 additions & 5 deletions packages/clerk-js/bundlewatch.config.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"files": [
{ "path": "./dist/clerk.js", "maxSize": "622KB" },
{ "path": "./dist/clerk.js", "maxSize": "624KB" },
{ "path": "./dist/clerk.browser.js", "maxSize": "76KB" },
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "117KB" },
{ "path": "./dist/clerk.headless*.js", "maxSize": "58KB" },
{ "path": "./dist/ui-common*.js", "maxSize": "113KB" },
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "118KB" },
{ "path": "./dist/clerk.headless*.js", "maxSize": "59KB" },
{ "path": "./dist/ui-common*.js", "maxSize": "114KB" },
{ "path": "./dist/ui-common*.legacy.*.js", "maxSize": "118KB" },
{ "path": "./dist/vendors*.js", "maxSize": "40.2KB" },
{ "path": "./dist/vendors*.js", "maxSize": "41KB" },
{ "path": "./dist/coinbase*.js", "maxSize": "38KB" },
{ "path": "./dist/stripe-vendors*.js", "maxSize": "1KB" },
{ "path": "./dist/createorganization*.js", "maxSize": "5KB" },
Expand Down
56 changes: 55 additions & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,32 @@ type SetActiveHook = (intent?: 'sign-out') => void | Promise<void>;

export type ClerkCoreBroadcastChannelEvent = { type: 'signout' };

/**
* Interface for the debug logger with all available logging methods
*/
interface DebugLoggerInterface {
debug(message: string, context?: Record<string, unknown>, source?: string): void;
error(message: string, context?: Record<string, unknown>, source?: string): void;
info(message: string, context?: Record<string, unknown>, source?: string): void;
trace(message: string, context?: Record<string, unknown>, source?: string): void;
warn(message: string, context?: Record<string, unknown>, source?: string): void;
}

/**
* Type guard to check if an object implements the DebugLoggerInterface
*/
function _isDebugLogger(obj: unknown): obj is DebugLoggerInterface {
return (
typeof obj === 'object' &&
obj !== null &&
typeof (obj as DebugLoggerInterface).debug === 'function' &&
typeof (obj as DebugLoggerInterface).error === 'function' &&
typeof (obj as DebugLoggerInterface).info === 'function' &&
typeof (obj as DebugLoggerInterface).trace === 'function' &&
typeof (obj as DebugLoggerInterface).warn === 'function'
);
}

declare global {
interface Window {
Clerk?: Clerk;
Expand Down Expand Up @@ -199,8 +225,8 @@ export class Clerk implements ClerkInterface {
public static sdkMetadata: SDKMetadata = {
name: __PKG_NAME__,
version: __PKG_VERSION__,
environment: process.env.NODE_ENV || 'production',
};

private static _billing: CommerceBillingNamespace;
private static _apiKeys: APIKeysNamespace;
private _checkout: ClerkInterface['__experimental_checkout'] | undefined;
Expand All @@ -212,6 +238,7 @@ export class Clerk implements ClerkInterface {
public __internal_country?: string | null;
public telemetry: TelemetryCollector | undefined;
public readonly __internal_state: State = new State();
public debugLogger?: DebugLoggerInterface;

protected internal_last_error: ClerkAPIError | null = null;
// converted to protected environment to support `updateEnvironment` type assertion
Expand Down Expand Up @@ -1509,6 +1536,7 @@ export class Clerk implements ClerkInterface {
const customNavigate =
options?.replace && this.#options.routerReplace ? this.#options.routerReplace : this.#options.routerPush;

this.debugLogger?.info(`Clerk is navigating to: ${toURL}`);
if (this.#options.routerDebug) {
console.log(`Clerk is navigating to: ${toURL}`);
}
Expand Down Expand Up @@ -2233,6 +2261,23 @@ export class Clerk implements ClerkInterface {

public updateEnvironment(environment: EnvironmentResource): asserts this is { environment: EnvironmentResource } {
this.environment = environment;

// Initialize debug module if client_debug_mode is enabled
if (environment.clientDebugMode) {
this.#initializeDebugModule();
}
}

async #initializeDebugModule(): Promise<void> {
try {
const { getDebugLogger } = await import('./modules/debug');
const logger = await getDebugLogger({});
if (_isDebugLogger(logger)) {
this.debugLogger = logger;
}
} catch (error) {
console.error('Failed to initialize debug module:', error);
}
}

__internal_setCountry = (country: string | null) => {
Expand Down Expand Up @@ -2893,4 +2938,13 @@ export class Clerk implements ClerkInterface {

return allowedProtocols;
}

/**
* @internal
*/
public static async __internal_resetDebugLogger(): Promise<void> {
// This method is now handled by the debug module itself
const { __internal_resetDebugLogger } = await import('./modules/debug');
__internal_resetDebugLogger();
}
}
Loading
Loading