You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is not a question, general help request, or anything other than a feature request directly related to Clerk. Please ask questions in our Discord community: https://clerk.com/discord.
Description
Clerk is almost 50% of my bundle size. It's built with Vite and I only use it like this:
It would be nice to have the react clerk package export Clerk so that I can initialize the client outside of React, then I wouldn't need to install the @clerk/clerk-js package.
I would love to see the separation of authentication logic (clerk.load(), clerk.user...) from the UI parts (clerk.mountSignIn()...). Bundle size seems to be mostly impacted by the UI parts and could only be imported when needed, e.g. a login page.
On another note @hornta , do you mind sharing why you have chose to import Clerk like that in an React application ?
What are you trying to access from clerk-js that clerk-react does not re-export or does not give you access through hooks ?
@panteliselef That decreases the size, although it's still the largest dependency which is weird because it's "just authentication" compared to for example the react footprint :-) But I'm OK with it being like this for now.
For some reason vscode couln't find out that the package exported that and there seems to be no typings for this headless as well, but it works at runtime and it can build it.
On another note @hornta , do you mind sharing why you have chose to import
I need to use Clerk outside of the "react world", for example in redux toolkit querys createApi and in react router dom createBrowserRouter.
I mean for what Clerk do, it being larger than or equally large as react feels like it has unoptimized code and/or imports things bundlers can't treeshake away. It's mostly communication with Clerk backend.
@panteliselef If using '@clerk/clerk-js/headless' what is the suggested way of loading the UI-Components later on?
I ended up having two instances of clerk in my app: 1 global instance ('@clerk/clerk-js/headless') and the other only on my login page ('@clerk/clerk-js')
@panteliselef If using '@clerk/clerk-js/headless' what is the suggested way of loading the UI-Components later on?
I ended up having two instances of clerk in my app: 1 global instance ('@clerk/clerk-js/headless') and the other only on my login page ('@clerk/clerk-js')
@fmoessle I believe the following would work for you
Activity
fmoessle commentedon Oct 8, 2023
I would love to see the separation of authentication logic (clerk.load(), clerk.user...) from the UI parts (clerk.mountSignIn()...). Bundle size seems to be mostly impacted by the UI parts and could only be imported when needed, e.g. a login page.
panteliselef commentedon Oct 8, 2023
Hello @fmoessle @hornta , could u give the following snippet a try ?
Instead of doing
import Clerk like this
This should bring the bundle size down significantly.
panteliselef commentedon Oct 8, 2023
On another note @hornta , do you mind sharing why you have chose to import Clerk like that in an React application ?
What are you trying to access from clerk-js that clerk-react does not re-export or does not give you access through hooks ?
langonja commentedon Oct 8, 2023
@panteliselef That decreases the size, although it's still the largest dependency which is weird because it's "just authentication" compared to for example the react footprint :-) But I'm OK with it being like this for now.
For some reason vscode couln't find out that the package exported that and there seems to be no typings for this headless as well, but it works at runtime and it can build it.
I need to use Clerk outside of the "react world", for example in redux toolkit querys
createApiand in react router domcreateBrowserRouter.langonja commentedon Oct 8, 2023
I mean for what Clerk do, it being larger than or equally large as react feels like it has unoptimized code and/or imports things bundlers can't treeshake away. It's mostly communication with Clerk backend.
fmoessle commentedon Oct 8, 2023
I also experience missing types. As a workaround, you can import the type like this
and tell typescript to handle the headless clerk instance to be of the type
fmoessle commentedon Oct 8, 2023
@panteliselef If using
'@clerk/clerk-js/headless'what is the suggested way of loading the UI-Components later on?I ended up having two instances of clerk in my app: 1 global instance (
'@clerk/clerk-js/headless') and the other only on my login page ('@clerk/clerk-js')panteliselef commentedon Oct 8, 2023
@fmoessle I believe the following would work for you