Skip to content

Commit 0804f62

Browse files
jacekradkowobsoriano
authored andcommitted
feat(clerk-js): Add debug logging to session update when offline (#7113)
1 parent d01533b commit 0804f62

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

.changeset/every-chefs-mix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Add debug logging to session update flows when browser is offline

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isValidBrowserOnline } from '@clerk/shared/browser';
12
import type { createClerkEventBus } from '@clerk/shared/clerkEventBus';
23
import { clerkEvents } from '@clerk/shared/clerkEventBus';
34
import type { createCookieHandler } from '@clerk/shared/cookie';
@@ -6,6 +7,8 @@ import { is4xxError, isClerkAPIResponseError, isClerkRuntimeError, isNetworkErro
67
import type { Clerk, InstanceType } from '@clerk/shared/types';
78
import { noop } from '@clerk/shared/utils';
89

10+
import { debugLogger } from '@/utils/debug';
11+
912
import { clerkMissingDevBrowserJwt } from '../errors';
1013
import { eventBus, events } from '../events';
1114
import type { FapiClient } from '../fapiClient';
@@ -174,6 +177,10 @@ export class AuthCookieService {
174177
return;
175178
}
176179

180+
if (!token && !isValidBrowserOnline()) {
181+
debugLogger.warn('Removing session cookie (offline)', { sessionId: this.clerk.session?.id }, 'authCookieService');
182+
}
183+
177184
this.setActiveContextInStorage();
178185

179186
return token ? this.sessionCookie.set(token) : this.sessionCookie.remove();

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,13 @@ export class Clerk implements ClerkInterface {
13801380
// getToken syncs __session and __client_uat to cookies using events.TokenUpdate dispatched event.
13811381
const token = await newSession?.getToken();
13821382
if (!token) {
1383+
if (!isValidBrowserOnline()) {
1384+
debugLogger.warn(
1385+
'Token is null when setting active session (offline)',
1386+
{ sessionId: newSession?.id },
1387+
'clerk',
1388+
);
1389+
}
13831390
eventBus.emit(events.TokenUpdate, { token: null });
13841391
}
13851392

@@ -2380,6 +2387,13 @@ export class Clerk implements ClerkInterface {
23802387
this.#setAccessors(session);
23812388

23822389
// A client response contains its associated sessions, along with a fresh token, so we dispatch a token update event.
2390+
if (!this.session?.lastActiveToken && !isValidBrowserOnline()) {
2391+
debugLogger.warn(
2392+
'No last active token when updating client (offline)',
2393+
{ sessionId: this.session?.id },
2394+
'clerk',
2395+
);
2396+
}
23832397
eventBus.emit(events.TokenUpdate, { token: this.session?.lastActiveToken });
23842398
}
23852399

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type {
88
DeletedObjectJSON,
99
} from '@clerk/shared/types';
1010

11+
import { debugLogger } from '@/utils/debug';
12+
1113
import { clerkMissingFapiClientInResources } from '../errors';
1214
import type { FapiClient, FapiRequestInit, FapiResponse, FapiResponseJSON, HTTPMethod } from '../fapiClient';
1315
import { FraudProtection } from '../fraudProtection';
@@ -98,7 +100,14 @@ export abstract class BaseResource {
98100
code: 'network_error',
99101
});
100102
} else if (!isValidBrowserOnline()) {
101-
console.warn(e);
103+
debugLogger.warn(
104+
'Network request failed while offline, returning null',
105+
{
106+
method: requestInit.method,
107+
path: requestInit.path,
108+
},
109+
'baseResource',
110+
);
102111
return null;
103112
} else {
104113
throw e;

packages/clerk-js/src/core/resources/__tests__/Token.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { InstanceType } from '@clerk/shared/types';
22
import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from 'vitest';
33

44
import { mockFetch, mockNetworkFailedFetch } from '@/test/core-fixtures';
5+
import { debugLogger } from '@/utils/debug';
56

67
import { SUPPORTED_FAPI_VERSION } from '../../constants';
78
import { createFapiClient } from '../../fapiClient';
@@ -50,7 +51,7 @@ describe('Token', () => {
5051
writable: true,
5152
value: false,
5253
});
53-
warnSpy = vi.spyOn(console, 'warn').mockReturnValue();
54+
warnSpy = vi.spyOn(debugLogger, 'warn').mockReturnValue();
5455
});
5556

5657
afterEach(() => {

0 commit comments

Comments
 (0)