Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 12 additions & 3 deletions apps/mobile/src/features/cloud/CloudAuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ClerkProvider, getClerkInstance } from "@clerk/expo";
import { resourceCache } from "@clerk/expo/resource-cache";
import { tokenCache } from "@clerk/expo/token-cache";
import type { ReactNode } from "react";

Expand All @@ -11,8 +12,12 @@ const publishableKey = cloudConfig.relay.url ? cloudConfig.clerk.publishableKey

if (publishableKey) {
// The same native singleton ClerkProvider renders with, so the service
// observes it for the app's lifetime without a React subscription.
cloudAccountService.attach(getClerkInstance({ publishableKey }));
// observes it for the app's lifetime without a React subscription. Clerk
// reads the resource cache only when this creates the singleton; without
// it, a launch load that fails offline is never retried.
cloudAccountService.attach(
getClerkInstance({ publishableKey, __experimental_resourceCache: resourceCache }),
);
} else {
// Without cloud config no account can ever activate, so drop anything a
// previous build with cloud enabled left registered.
Expand All @@ -25,7 +30,11 @@ export function CloudAuthProvider(props: { readonly children: ReactNode }) {
}

return (
<ClerkProvider publishableKey={publishableKey} tokenCache={tokenCache}>
<ClerkProvider
publishableKey={publishableKey}
tokenCache={tokenCache}
__experimental_resourceCache={resourceCache}
>
{props.children}
</ClerkProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ describe("createCloudAccountService", () => {
const { source, emit } = makeSource();
service.attach(source);

// Offline: an id-less client, or the placeholder built from the session cookie.
// Offline: an id-less client, the placeholder built from the session cookie,
// or the placeholder Expo's empty resource cache loads.
emit(null, "");
emit(null, "client_init");
emit(null, "client_DUMMY_ID");
expect(service.getState()).toEqual({ _tag: "Unobserved" });
expect(log).toEqual([]);

Expand Down
8 changes: 5 additions & 3 deletions packages/client-runtime/src/relay/cloudAccountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ export interface CloudAccountSource {

/**
* Clerk's load status cannot prove its API answered: offline it emits an
* id-less client, and a failed launch fetch emits a "client_init" placeholder
* built from the session cookie. Only a real /v1/client response has an id.
* id-less client, a failed launch fetch emits a "client_init" placeholder
* built from the session cookie, and Expo's resource cache starts empty with a
* "client_DUMMY_ID" placeholder. Only a real /v1/client response has an id.
*/
const PLACEHOLDER_CLIENT_IDS = new Set(["client_init", "client_DUMMY_ID"]);
const isClientFromClerkApi = (
client: { readonly id?: string | null | undefined } | null | undefined,
) => Boolean(client?.id) && client?.id !== "client_init";
) => Boolean(client?.id) && !PLACEHOLDER_CLIENT_IDS.has(client?.id ?? "");

interface CloudAccountSignIn {
readonly _tag: "SignIn";
Expand Down
Loading