-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathcreate-injection-script-runner.ts
31 lines (26 loc) · 1.17 KB
/
create-injection-script-runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { $initialState } from '../stores/internal';
import type { AstroClerkIntegrationParams } from '../types';
import { mergeEnvVarsWithParams } from './merge-env-vars-with-params';
import type { CreateClerkInstanceInternalFn } from './types';
/**
* @internal
* Before initializing Clerk do:
* 1) Populate stores with the authentication state during SSR.
* 2) Merge the environment variables from the server context with the ones from the integration.
*/
function createInjectionScriptRunner(creator: CreateClerkInstanceInternalFn) {
async function runner(astroClerkOptions?: AstroClerkIntegrationParams) {
const ssrDataContainer = document.getElementById('__CLERK_ASTRO_DATA__');
if (ssrDataContainer) {
$initialState.set(JSON.parse(ssrDataContainer.textContent || '{}'));
}
const clientSafeVarsContainer = document.getElementById('__CLERK_ASTRO_SAFE_VARS__');
let clientSafeVars = {};
if (clientSafeVarsContainer) {
clientSafeVars = JSON.parse(clientSafeVarsContainer.textContent || '{}');
}
await creator(mergeEnvVarsWithParams({ ...astroClerkOptions, ...clientSafeVars }));
}
return runner;
}
export { createInjectionScriptRunner };