Skip to content

Commit d3a360a

Browse files
committed
use loadenv
1 parent f7d0ce9 commit d3a360a

4 files changed

Lines changed: 32 additions & 5 deletions

File tree

packages/astro/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
},
9797
"devDependencies": {
9898
"@clerk/ui": "workspace:^",
99-
"astro": "^5.15.9"
99+
"astro": "^5.15.9",
100+
"vite": "^7.1.0"
100101
},
101102
"peerDependencies": {
102103
"astro": "^4.15.0 || ^5.0.0"

packages/astro/src/integration/create-integration.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ClerkOptions } from '@clerk/shared/types';
22
import type { AstroIntegration } from 'astro';
33
import { envField } from 'astro/config';
4+
import { loadEnv } from 'vite';
45

56
import { name as packageName, version as packageVersion } from '../../package.json';
67
import { resolveKeysWithKeylessFallback } from '../server/keyless/utils';
@@ -32,10 +33,24 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
3233
logger.error('Missing adapter, please update your Astro config to use one.');
3334
}
3435

35-
const envPublishableKey = process.env.PUBLIC_CLERK_PUBLISHABLE_KEY;
36-
const envSecretKey = process.env.CLERK_SECRET_KEY;
37-
3836
const isDev = command === 'dev';
37+
38+
// Load environment variables from .env files
39+
// Astro's integration hook runs before .env is loaded, so we need to do it manually
40+
const mode = isDev ? 'development' : 'production';
41+
const envDir = config.root.pathname;
42+
const loadedEnv = loadEnv(mode, envDir, '');
43+
44+
console.log('[Integration Debug] Loaded env from .env files:', {
45+
PUBLIC_CLERK_PUBLISHABLE_KEY: loadedEnv.PUBLIC_CLERK_PUBLISHABLE_KEY?.substring(0, 20) + '...',
46+
CLERK_SECRET_KEY: loadedEnv.CLERK_SECRET_KEY?.substring(0, 20) + '...',
47+
allClerkKeys: Object.keys(loadedEnv).filter(k => k.includes('CLERK')),
48+
});
49+
50+
// Try .env files first, then fall back to process.env
51+
const envPublishableKey = loadedEnv.PUBLIC_CLERK_PUBLISHABLE_KEY || process.env.PUBLIC_CLERK_PUBLISHABLE_KEY;
52+
const envSecretKey = loadedEnv.CLERK_SECRET_KEY || process.env.CLERK_SECRET_KEY;
53+
3954
let resolvedKeys = {
4055
publishableKey: envPublishableKey,
4156
secretKey: envSecretKey,

packages/astro/tsup.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export default defineConfig(() => {
2626
bundle: true,
2727
sourcemap: true,
2828
format: ['esm'],
29-
external: ['astro', 'react', 'react-dom', 'node:async_hooks', '#async-local-storage', 'astro:transitions/client'],
29+
external: [
30+
'astro',
31+
'react',
32+
'react-dom',
33+
'node:async_hooks',
34+
'#async-local-storage',
35+
'astro:transitions/client',
36+
'vite',
37+
],
3038
};
3139
});

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)