-
Notifications
You must be signed in to change notification settings - Fork 433
chore(astro,shared): Move functions that can be reused by different SDKs #3681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b1bfa60
fcae45e
e60b635
a05b2c8
542da2d
696b221
401cc89
2e4ecc8
69fa644
a40a55d
f159c5e
6af9ea5
df670e1
1cd7611
1235e8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@clerk/astro": patch | ||
| "@clerk/shared": patch | ||
| --- | ||
|
|
||
| Introduce functions that can be reused across front-end SDKs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| import { waitForClerkScript } from '../internal/utils/loadClerkJSScript'; | ||
| import { loadClerkJsScript } from '@clerk/shared/loadClerkJsScript'; | ||
|
|
||
| import { $clerk, $csrState } from '../stores/internal'; | ||
| import type { AstroClerkIntegrationParams, AstroClerkUpdateOptions } from '../types'; | ||
| import { mountAllClerkAstroJSComponents } from './mount-clerk-astro-js-components'; | ||
| import { runOnce } from './run-once'; | ||
|
|
||
| let initOptions: AstroClerkIntegrationParams | undefined; | ||
|
|
||
| const HARDCODED_LATEST_CLERK_JS_VERSION = '5'; | ||
|
|
||
| /** | ||
| * Prevents firing clerk.load multiple times | ||
| */ | ||
|
|
@@ -14,7 +17,10 @@ export const createClerkInstance = runOnce(createClerkInstanceInternal); | |
| export async function createClerkInstanceInternal(options?: AstroClerkIntegrationParams) { | ||
| let clerkJSInstance = window.Clerk; | ||
| if (!clerkJSInstance) { | ||
| await waitForClerkScript(); | ||
| await loadClerkJsScript({ | ||
| clerkJSVersion: HARDCODED_LATEST_CLERK_JS_VERSION, | ||
| ...(options as any), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| }); | ||
|
|
||
| if (!window.Clerk) { | ||
| throw new Error('Failed to download latest ClerkJS. Contact [email protected].'); | ||
|
|
@@ -23,13 +29,11 @@ export async function createClerkInstanceInternal(options?: AstroClerkIntegratio | |
| } | ||
|
|
||
| if (!$clerk.get()) { | ||
| // @ts-ignore | ||
| $clerk.set(clerkJSInstance); | ||
| } | ||
|
|
||
| initOptions = options; | ||
| // TODO: Update Clerk type from @clerk/types to include this method | ||
| return (clerkJSInstance as any) | ||
| return clerkJSInstance | ||
| .load(options) | ||
| .then(() => { | ||
| $csrState.setKey('isLoaded', true); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,21 @@ const mergeEnvVarsWithParams = (params?: AstroClerkIntegrationParams & { publish | |
| ...rest | ||
| } = params || {}; | ||
|
|
||
| // We have an eslint config that requires us to declare env variables in the turbo.json file. | ||
| // We're skipping/disabling it below as we use it only for the Astro integration. | ||
wobsoriano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { | ||
| signInUrl: paramSignIn || import.meta.env.PUBLIC_ASTRO_APP_CLERK_SIGN_IN_URL, | ||
| signUpUrl: paramSignUp || import.meta.env.PUBLIC_ASTRO_APP_CLERK_SIGN_UP_URL, | ||
| isSatellite: paramSatellite || import.meta.env.PUBLIC_ASTRO_APP_CLERK_IS_SATELLITE, | ||
| proxyUrl: paramProxy || import.meta.env.PUBLIC_ASTRO_APP_CLERK_PROXY_URL, | ||
| domain: paramDomain || import.meta.env.PUBLIC_ASTRO_APP_CLERK_DOMAIN, | ||
| publishableKey: paramPublishableKey || import.meta.env.PUBLIC_ASTRO_APP_CLERK_PUBLISHABLE_KEY || '', | ||
| // eslint-disable-next-line turbo/no-undeclared-env-vars | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Use block comments (https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) because then it's more concise |
||
| signInUrl: paramSignIn || import.meta.env.PUBLIC_CLERK_SIGN_IN_URL, | ||
| // eslint-disable-next-line turbo/no-undeclared-env-vars | ||
| signUpUrl: paramSignUp || import.meta.env.PUBLIC_CLERK_SIGN_UP_URL, | ||
| // eslint-disable-next-line turbo/no-undeclared-env-vars | ||
| isSatellite: paramSatellite || import.meta.env.PUBLIC_CLERK_IS_SATELLITE, | ||
| // eslint-disable-next-line turbo/no-undeclared-env-vars | ||
| proxyUrl: paramProxy || import.meta.env.PUBLIC_CLERK_PROXY_URL, | ||
| // eslint-disable-next-line turbo/no-undeclared-env-vars | ||
| domain: paramDomain || import.meta.env.PUBLIC_CLERK_DOMAIN, | ||
| // eslint-disable-next-line turbo/no-undeclared-env-vars | ||
| publishableKey: paramPublishableKey || import.meta.env.PUBLIC_CLERK_PUBLISHABLE_KEY || '', | ||
| ...rest, | ||
| }; | ||
| }; | ||
|
|
||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,6 @@ import type { APIContext } from 'astro'; | |
| // @ts-ignore | ||
| import { authAsyncStorage } from '#async-local-storage'; | ||
|
|
||
| import { buildClerkHotloadScript } from './build-clerk-hotload-script'; | ||
| import { clerkClient } from './clerk-client'; | ||
| import { createCurrentUser } from './current-user'; | ||
| import { getAuth } from './get-auth'; | ||
|
|
@@ -263,7 +262,6 @@ async function decorateRequest( | |
| const clerkSafeEnvVariables = encoder.encode( | ||
| `<script id="__CLERK_ASTRO_SAFE_VARS__" type="application/json">${JSON.stringify(getClientSafeEnv(locals))}</script>\n`, | ||
| ); | ||
| const hotloadScript = encoder.encode(buildClerkHotloadScript(locals)); | ||
|
|
||
| const stream = res.body!.pipeThrough( | ||
| new TransformStream({ | ||
|
|
@@ -279,10 +277,6 @@ async function decorateRequest( | |
| controller.enqueue(clerkAstroData); | ||
| controller.enqueue(clerkSafeEnvVariables); | ||
|
|
||
| if (__HOTLOAD__) { | ||
| controller.enqueue(hotloadScript); | ||
| } | ||
|
|
||
|
Comment on lines
-282
to
-285
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are no longer attaching the hotload script to the Response since we're using the If for some reason the current implementation is performant than this one, we can adjust to 3-4 commits back where I only used the script builders to clean up some repeating logic but still build the hotload script on the server
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We definitely prefer the current implementation because it will result in faster loading times.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can still use |
||
| controller.enqueue(closingHeadTag); | ||
| controller.enqueue(chunk.slice(index + closingHeadTag.length)); | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import type { InitialState, Resources } from '@clerk/types'; | ||
|
|
||
| import { deriveState } from '../deriveState'; | ||
|
|
||
| describe('deriveState', () => { | ||
| const mockInitialState = { | ||
| userId: 'user_2U330vGHg3llBga8Oi0fzzeNAaG', | ||
| sessionId: 'sess_2j1R7g3AUeKMx9M23dBO0XLEQGY', | ||
| orgId: 'org_2U330vGHg3llBga8Oi0fzzeNAaG', | ||
| } as InitialState; | ||
|
|
||
| const mockResources = { | ||
| client: {}, | ||
| user: { id: mockInitialState.userId }, | ||
| session: { id: mockInitialState.sessionId }, | ||
| organization: { id: mockInitialState.orgId }, | ||
| } as Resources; | ||
|
|
||
| it('uses SSR state when !clerkLoaded and initialState is provided', () => { | ||
| expect(deriveState(false, {} as Resources, mockInitialState)).toEqual(mockInitialState); | ||
| }); | ||
|
|
||
| it('uses CSR state when clerkLoaded', () => { | ||
| const result = deriveState(true, mockResources, undefined); | ||
| expect(result.userId).toBe(mockInitialState.userId); | ||
| expect(result.sessionId).toBe(mockInitialState.sessionId); | ||
| expect(result.orgId).toBe(mockInitialState.orgId); | ||
| }); | ||
|
|
||
| it('handles !clerkLoaded and undefined initialState', () => { | ||
| const result = deriveState(false, {} as Resources, undefined); | ||
| expect(result.userId).toBeUndefined(); | ||
| expect(result.sessionId).toBeUndefined(); | ||
| expect(result.orgId).toBeUndefined(); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to remove this, I'll explain it in https://github.com/clerk/javascript/pull/3681/files#r1672868119