|
1 | | -import type { ClerkOptions } from '@clerk/types'; |
| 1 | +import type { ClerkOptions } from '@clerk/shared/types'; |
2 | 2 | import type { AstroIntegration } from 'astro'; |
3 | 3 | import { envField } from 'astro/config'; |
4 | 4 |
|
5 | 5 | import { name as packageName, version as packageVersion } from '../../package.json'; |
6 | 6 | import type { AstroClerkIntegrationParams } from '../types'; |
| 7 | +import { buildBeforeHydrationSnippet, buildPageLoadSnippet } from './snippets'; |
7 | 8 | import { vitePluginAstroConfig } from './vite-plugin-astro-config'; |
8 | 9 |
|
9 | | -const buildEnvVarFromOption = (valueToBeStored: unknown, envName: keyof InternalEnv) => { |
10 | | - return valueToBeStored ? { [`import.meta.env.${envName}`]: JSON.stringify(valueToBeStored) } : {}; |
11 | | -}; |
12 | | - |
13 | 10 | type HotloadAstroClerkIntegrationParams = AstroClerkIntegrationParams & { |
14 | 11 | clerkJSUrl?: string; |
15 | 12 | clerkJSVariant?: 'headless' | ''; |
16 | 13 | clerkJSVersion?: string; |
17 | 14 | enableEnvSchema?: boolean; |
18 | 15 | }; |
19 | 16 |
|
| 17 | +const buildEnvVarFromOption = (valueToBeStored: unknown, envName: keyof InternalEnv) => { |
| 18 | + return valueToBeStored ? { [`import.meta.env.${envName}`]: JSON.stringify(valueToBeStored) } : {}; |
| 19 | +}; |
| 20 | + |
20 | 21 | function createIntegration<Params extends HotloadAstroClerkIntegrationParams>() { |
21 | 22 | return (params?: Params): AstroIntegration => { |
22 | 23 | const { proxyUrl, isSatellite, domain, signInUrl, signUpUrl, enableEnvSchema = true } = params || {}; |
@@ -95,63 +96,32 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>() |
95 | 96 | */ |
96 | 97 |
|
97 | 98 | /** |
98 | | - * The above script will run before client frameworks like React hydrate. |
| 99 | + * The before-hydration script will run before client frameworks like React hydrate. |
99 | 100 | * This makes sure that we have initialized a Clerk instance and populated stores in order to avoid hydration issues. |
100 | 101 | */ |
101 | 102 | injectScript( |
102 | 103 | 'before-hydration', |
103 | | - ` |
104 | | - ${command === 'dev' ? `console.log('${packageName}',"Initialize Clerk: before-hydration")` : ''} |
105 | | - import { runInjectionScript } from "${buildImportPath}"; |
106 | | - await runInjectionScript(${JSON.stringify(internalParams)});`, |
| 104 | + buildBeforeHydrationSnippet({ |
| 105 | + command, |
| 106 | + packageName, |
| 107 | + buildImportPath, |
| 108 | + internalParams, |
| 109 | + }), |
107 | 110 | ); |
108 | 111 |
|
109 | 112 | /** |
110 | | - * The above script only executes if a client framework like React needs to hydrate. |
111 | | - * We need to run the same script again for each page in order to initialize Clerk even if no UI framework is used in the client |
112 | | - * If no UI framework is used in the client, the above script with `before-hydration` will never run |
| 113 | + * The page script only executes if a client framework like React needs to hydrate. |
| 114 | + * We need to run the same script again for each page in order to initialize Clerk even if no UI framework is used in the client. |
| 115 | + * If no UI framework is used in the client, the before-hydration script will never run. |
113 | 116 | */ |
114 | | - |
115 | 117 | injectScript( |
116 | 118 | 'page', |
117 | | - ` |
118 | | - ${command === 'dev' ? `console.log("${packageName}","Initialize Clerk: page")` : ''} |
119 | | - import { runInjectionScript, swapDocument } from "${buildImportPath}"; |
120 | | -
|
121 | | - // Taken from https://github.com/withastro/astro/blob/e10b03e88c22592fbb42d7245b65c4f486ab736d/packages/astro/src/transitions/router.ts#L39. |
122 | | - // Importing it directly from astro:transitions/client breaks custom client-side routing |
123 | | - // even when View Transitions is disabled. |
124 | | - const transitionEnabledOnThisPage = () => { |
125 | | - return !!document.querySelector('[name="astro-view-transitions-enabled"]'); |
126 | | - } |
127 | | -
|
128 | | - if (transitionEnabledOnThisPage()) { |
129 | | - // We must do the dynamic imports within the event listeners because otherwise we may race and miss initial astro:page-load |
130 | | - document.addEventListener('astro:before-swap', async (e) => { |
131 | | - const { swapFunctions } = await import('astro:transitions/client'); |
132 | | -
|
133 | | - const clerkComponents = document.querySelector('#clerk-components'); |
134 | | - // Keep the div element added by Clerk |
135 | | - if (clerkComponents) { |
136 | | - const clonedEl = clerkComponents.cloneNode(true); |
137 | | - e.newDocument.body.appendChild(clonedEl); |
138 | | - } |
139 | | -
|
140 | | - e.swap = () => swapDocument(swapFunctions, e.newDocument); |
141 | | - }); |
142 | | -
|
143 | | - document.addEventListener('astro:page-load', async (e) => { |
144 | | - const { navigate } = await import('astro:transitions/client'); |
145 | | -
|
146 | | - await runInjectionScript({ |
147 | | - ...${JSON.stringify(internalParams)}, |
148 | | - routerPush: navigate, |
149 | | - routerReplace: (url) => navigate(url, { history: 'replace' }), |
150 | | - }); |
151 | | - }) |
152 | | - } else { |
153 | | - await runInjectionScript(${JSON.stringify(internalParams)}); |
154 | | - }`, |
| 119 | + buildPageLoadSnippet({ |
| 120 | + command, |
| 121 | + packageName, |
| 122 | + buildImportPath, |
| 123 | + internalParams, |
| 124 | + }), |
155 | 125 | ); |
156 | 126 | }, |
157 | 127 | 'astro:config:done': ({ injectTypes }) => { |
|
0 commit comments