Skip to content

Commit

Permalink
fix: astro:env getSecret (#11296)
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre authored Jun 20, 2024
1 parent e67d7e6 commit 5848d97
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/small-sloths-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/vercel': patch
'@astrojs/node': patch
---

Fixes `astro:env` getSecret compatibility
17 changes: 16 additions & 1 deletion packages/integrations/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export function getAdapter(options: Options): AstroAdapter {
};
}

// TODO: remove once we don't use a TLA anymore
async function shouldExternalizeAstroEnvSetup() {
try {
await import('astro/env/setup');
return false;
} catch {
return true;
}
}

export default function createIntegration(userOptions: UserOptions): AstroIntegration {
if (!userOptions?.mode) {
throw new AstroError(`Setting the 'mode' option is required.`);
Expand All @@ -33,14 +43,19 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
return {
name: '@astrojs/node',
hooks: {
'astro:config:setup': ({ updateConfig, config }) => {
'astro:config:setup': async ({ updateConfig, config }) => {
updateConfig({
image: {
endpoint: config.image.endpoint ?? 'astro/assets/endpoint/node',
},
vite: {
ssr: {
noExternal: ['@astrojs/node'],
...((await shouldExternalizeAstroEnvSetup())
? {
external: ['astro/env/setup'],
}
: {}),
},
},
});
Expand Down
7 changes: 2 additions & 5 deletions packages/integrations/node/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import { createStandaloneHandler } from './standalone.js';
import startServer from './standalone.js';
import type { Options } from './types.js';

type EnvSetupModule = typeof import('astro/env/setup');

// Won't throw if the virtual module is not available because it's not supported in
// the users's astro version or if astro:env is not enabled in the project
const setupModule = 'astro/env/setup';
await import(/* @vite-ignore */ setupModule)
.then((mod: EnvSetupModule) => mod.setGetEnv((key) => process.env[key]))
await import('astro/env/setup')
.then((mod) => mod.setGetEnv((key) => process.env[key]))
.catch(() => {});

applyPolyfills();
Expand Down
15 changes: 14 additions & 1 deletion packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ export default function vercelServerless({
vite: {
...getSpeedInsightsViteConfig(speedInsights?.enabled),
ssr: {
external: ['@vercel/nft'],
external: [
'@vercel/nft',
...((await shouldExternalizeAstroEnvSetup()) ? ['astro/env/setup'] : []),
],
},
},
...getAstroImageConfig(
Expand Down Expand Up @@ -442,6 +445,16 @@ export default function vercelServerless({

type Runtime = `nodejs${string}.x`;

// TODO: remove once we don't use a TLA anymore
async function shouldExternalizeAstroEnvSetup() {
try {
await import('astro/env/setup');
return false;
} catch {
return true;
}
}

class VercelBuilder {
readonly NTF_CACHE = {};

Expand Down
7 changes: 2 additions & 5 deletions packages/integrations/vercel/src/serverless/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import {
ASTRO_PATH_PARAM,
} from './adapter.js';

type EnvSetupModule = typeof import('astro/env/setup');

// Won't throw if the virtual module is not available because it's not supported in
// the users's astro version or if astro:env is not enabled in the project
const setupModule = 'astro/env/setup';
await import(/* @vite-ignore */ setupModule)
.then((mod: EnvSetupModule) => mod.setGetEnv((key) => process.env[key]))
await import('astro/env/setup')
.then((mod) => mod.setGetEnv((key) => process.env[key]))
.catch(() => {});

applyPolyfills();
Expand Down

0 comments on commit 5848d97

Please sign in to comment.