Skip to content
Merged
8 changes: 8 additions & 0 deletions .changeset/fifty-bananas-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@clerk/clerk-js': minor
'@clerk/types': minor
---

Introduce the `experimental.rethrowOfflineNetworkErrors` option to the `ClerkProvider` component.

When set to `true`, Clerk will rethrow network errors that occur while the user is offline.
14 changes: 12 additions & 2 deletions packages/clerk-js/src/core/resources/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ClerkAPIErrorJSON, ClerkResourceJSON, ClerkResourceReloadParams, D
import { clerkMissingFapiClientInResources } from '../errors';
import type { FapiClient, FapiRequestInit, FapiResponse, FapiResponseJSON, HTTPMethod } from '../fapiClient';
import type { Clerk } from './internal';
import { ClerkAPIResponseError, Client } from './internal';
import { ClerkAPIResponseError, ClerkRuntimeError, Client } from './internal';

export type BaseFetchOptions = ClerkResourceReloadParams & { forceUpdateClient?: boolean };

Expand Down Expand Up @@ -62,7 +62,12 @@ export abstract class BaseResource {
try {
fapiResponse = await BaseResource.fapiClient.request<J>(requestInit);
} catch (e) {
if (!isValidBrowserOnline()) {
// TODO: This should be the default behavior in the next major version, as long as we have a way to handle the requests more gracefully when offline
if (this.shouldRethrowOfflineNetworkErrors()) {
throw new ClerkRuntimeError(e?.message || e, {
code: 'network_error',
});
} else if (!isValidBrowserOnline()) {
console.warn(e);
return null;
} else {
Expand Down Expand Up @@ -187,4 +192,9 @@ export abstract class BaseResource {
const { rotatingTokenNonce } = params || {};
return this._baseGet({ forceUpdateClient: true, rotatingTokenNonce });
}

private static shouldRethrowOfflineNetworkErrors(): boolean {
const experimental = BaseResource.clerk?.__internal_getOption?.('experimental');
return experimental?.rethrowOfflineNetworkErrors || false;
Comment thread
anagstef marked this conversation as resolved.
}
}
1 change: 1 addition & 0 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ export type ClerkOptions = ClerkOptionsNavigation &
experimental?: Autocomplete<
{
persistClient: boolean;
rethrowOfflineNetworkErrors: boolean;
},
Record<string, any>
>;
Expand Down