While ClerkJS can be used in any browser context and framework, realistically users expect to consume its features through the conventions and syntax of their framework of choice. For example, @clerk/clerk-react turns ClerkJS into React components, @clerk/astro into Astro components, and so on.
In non-browser environments, you’ll need to re-implement the Clerk class in the SDK’s programming language, interacting with the FAPI.
While the implementation details will vary for each SDK, there are certain steps you'll have to go through in any case. Consider the steps below a rough guidance on what needs to be done, and also remember to follow the conventionsNext.js Icon.
Note
The code blocks below will be written in pseudo-code. If you're looking for real-world examples, have a look at these repositories: @clerk/clerk-react, @clerk/astro
Create a Clerk instance that will only be invoked once (e.g. following the singleton pattern). During its initialization you'll execute the following steps.
create-clerk-instance.ts
import { runOnce } from'./utils'// States accessible to other parts of your SDK and/or its usersimport { $clerk, $state } from'./stores'exportconstcreateClerkInstance=runOnce(createClerkInstanceInternal)asyncfunctioncreateClerkInstanceInternal(options) {let clerkJSInstance =window.Clerk}
Build out the components that your users will utilize in their app. Call the mount() function when the component is in view/mounts and the unmount() function when the component is unmounted. These functions are described in the Clerk class componentsJavaScript Icon.
Use the idiomatic way of your framework for doing this. If you can abstract these repetitions into a re-usable hook/directive, then do that.