From 56178e2a16a746218c3c3051e2f19e8ad28eb15c Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Tue, 10 Sep 2024 15:07:20 +0200 Subject: [PATCH 1/2] chore(next): astro:env cleanup --- .changeset/sixty-trainers-shout.md | 7 ++ packages/netlify/src/index.ts | 15 ---- packages/netlify/src/ssr-function.ts | 8 +-- packages/node/src/index.ts | 15 ---- packages/vercel/package.json | 18 ++--- packages/vercel/src/serverless/adapter.ts | 85 ++++++++++------------- 6 files changed, 49 insertions(+), 99 deletions(-) create mode 100644 .changeset/sixty-trainers-shout.md diff --git a/.changeset/sixty-trainers-shout.md b/.changeset/sixty-trainers-shout.md new file mode 100644 index 000000000..8838fdede --- /dev/null +++ b/.changeset/sixty-trainers-shout.md @@ -0,0 +1,7 @@ +--- +'@astrojs/netlify': minor +'@astrojs/vercel': minor +'@astrojs/node': minor +--- + +Cleans up `astro:env` support diff --git a/packages/netlify/src/index.ts b/packages/netlify/src/index.ts index d2da0fd98..9c6173f1a 100644 --- a/packages/netlify/src/index.ts +++ b/packages/netlify/src/index.ts @@ -135,16 +135,6 @@ async function writeNetlifyFrameworkConfig(config: AstroConfig, logger: AstroInt ); } -// 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 interface NetlifyIntegrationConfig { /** * If enabled, On-Demand-Rendered pages are cached for up to a year. @@ -427,11 +417,6 @@ export default function netlifyIntegration( ignored: [fileURLToPath(new URL('./.netlify/**', rootDir))], }, }, - ...((await shouldExternalizeAstroEnvSetup()) - ? { - ssr: { external: ['astro/env/setup'] }, - } - : {}), }, image: { service: { diff --git a/packages/netlify/src/ssr-function.ts b/packages/netlify/src/ssr-function.ts index 22925ddaa..c8502cbe7 100644 --- a/packages/netlify/src/ssr-function.ts +++ b/packages/netlify/src/ssr-function.ts @@ -2,14 +2,10 @@ import type { Context } from '@netlify/functions'; import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; import { applyPolyfills } from 'astro/app/node'; +import { setGetEnv } from 'astro/env/setup'; applyPolyfills(); - -// 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 -await import('astro/env/setup') - .then((mod) => mod.setGetEnv((key) => process.env[key])) - .catch(() => {}); +setGetEnv((key) => process.env[key]); export interface Args { middlewareSecret: string; diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index ec7a2ae3f..4e03bd595 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -23,16 +23,6 @@ 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.`); @@ -50,11 +40,6 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr vite: { ssr: { noExternal: ['@astrojs/node'], - ...((await shouldExternalizeAstroEnvSetup()) - ? { - external: ['astro/env/setup'], - } - : {}), }, }, }); diff --git a/packages/vercel/package.json b/packages/vercel/package.json index 1a9785f52..3c9b81a69 100644 --- a/packages/vercel/package.json +++ b/packages/vercel/package.json @@ -10,10 +10,7 @@ "url": "https://github.com/withastro/adapters.git", "directory": "packages/vercel" }, - "keywords": [ - "withastro", - "astro-adapter" - ], + "keywords": ["withastro", "astro-adapter"], "bugs": "https://github.com/withastro/adapters/issues", "homepage": "https://docs.astro.build/en/guides/integrations-guide/vercel/", "exports": { @@ -30,18 +27,11 @@ }, "typesVersions": { "*": { - "serverless": [ - "dist/serverless/adapter.d.ts" - ], - "static": [ - "dist/static/adapter.d.ts" - ] + "serverless": ["dist/serverless/adapter.d.ts"], + "static": ["dist/static/adapter.d.ts"] } }, - "files": [ - "dist", - "types.d.ts" - ], + "files": ["dist", "types.d.ts"], "scripts": { "build": "tsc", "test": "astro-scripts test --timeout 50000 \"test/**/!(hosted).test.js\"", diff --git a/packages/vercel/src/serverless/adapter.ts b/packages/vercel/src/serverless/adapter.ts index a762103f9..1b5547010 100644 --- a/packages/vercel/src/serverless/adapter.ts +++ b/packages/vercel/src/serverless/adapter.ts @@ -235,10 +235,10 @@ export default function vercelServerless({ if (vercelConfig.trailingSlash === true && config.trailingSlash === 'always') { logger.warn( '\n' + - `\tYour "vercel.json" \`trailingSlash\` configuration (set to \`true\`) will conflict with your Astro \`trailinglSlash\` configuration (set to \`"always"\`).\n` + - // biome-ignore lint/style/noUnusedTemplateLiteral: - `\tThis would cause infinite redirects under certain conditions and throw an \`ERR_TOO_MANY_REDIRECTS\` error.\n` + - `\tTo prevent this, your Astro configuration is updated to \`"ignore"\` during builds.\n` + `\tYour "vercel.json" \`trailingSlash\` configuration (set to \`true\`) will conflict with your Astro \`trailinglSlash\` configuration (set to \`"always"\`).\n` + + // biome-ignore lint/style/noUnusedTemplateLiteral: + `\tThis would cause infinite redirects under certain conditions and throw an \`ERR_TOO_MANY_REDIRECTS\` error.\n` + + `\tTo prevent this, your Astro configuration is updated to \`"ignore"\` during builds.\n` ); updateConfig({ trailingSlash: 'ignore', @@ -259,10 +259,7 @@ export default function vercelServerless({ vite: { ...getSpeedInsightsViteConfig(speedInsights?.enabled), ssr: { - external: [ - '@vercel/nft', - ...((await shouldExternalizeAstroEnvSetup()) ? ['astro/env/setup'] : []), - ], + external: ['@vercel/nft'], }, }, ...getAstroImageConfig( @@ -394,31 +391,31 @@ export default function vercelServerless({ ...routeDefinitions, ...(fourOhFourRoute ? [ - { - src: '/.*', - dest: fourOhFourRoute.prerender - ? '/404.html' - : _middlewareEntryPoint - ? MIDDLEWARE_PATH - : NODE_PATH, - status: 404, - }, - ] + { + src: '/.*', + dest: fourOhFourRoute.prerender + ? '/404.html' + : _middlewareEntryPoint + ? MIDDLEWARE_PATH + : NODE_PATH, + status: 404, + }, + ] : []), ], ...(imageService || imagesConfig ? { - images: imagesConfig - ? { - ...imagesConfig, - domains: [...imagesConfig.domains, ..._config.image.domains], - remotePatterns: [ - ...(imagesConfig.remotePatterns ?? []), - ..._config.image.remotePatterns, - ], - } - : getDefaultImageConfig(_config.image), - } + images: imagesConfig + ? { + ...imagesConfig, + domains: [...imagesConfig.domains, ..._config.image.domains], + remotePatterns: [ + ...(imagesConfig.remotePatterns ?? []), + ..._config.image.remotePatterns, + ], + } + : getDefaultImageConfig(_config.image), + } : {}), }); @@ -431,16 +428,6 @@ 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 = {}; @@ -451,7 +438,7 @@ class VercelBuilder { readonly logger: AstroIntegrationLogger, readonly maxDuration?: number, readonly runtime = getRuntime(process, logger) - ) { } + ) {} async buildServerlessFolder(entry: URL, functionName: string) { const { config, includeFiles, excludeFiles, logger, NTF_CACHE, runtime, maxDuration } = this; @@ -531,11 +518,11 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru // biome-ignore lint/style/useTemplate: // biome-ignore lint/style/noUnusedTemplateLiteral: `\n` + - `\tThe local Node.js version (${major}) is not supported by Vercel Serverless Functions.\n` + - // biome-ignore lint/style/noUnusedTemplateLiteral: - `\tYour project will use Node.js 18 as the runtime instead.\n` + - // biome-ignore lint/style/noUnusedTemplateLiteral: - `\tConsider switching your local version to 18.\n` + `\tThe local Node.js version (${major}) is not supported by Vercel Serverless Functions.\n` + + // biome-ignore lint/style/noUnusedTemplateLiteral: + `\tYour project will use Node.js 18 as the runtime instead.\n` + + // biome-ignore lint/style/noUnusedTemplateLiteral: + `\tConsider switching your local version to 18.\n` ); return 'nodejs18.x'; } @@ -564,10 +551,10 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru // biome-ignore lint/style/useTemplate: // biome-ignore lint/style/noUnusedTemplateLiteral: `\n` + - `\tYour project is being built for Node.js ${major} as the runtime.\n` + - `\tThis version is deprecated by Vercel Serverless Functions, and scheduled to be disabled on ${removeDate}.\n` + - // biome-ignore lint/style/noUnusedTemplateLiteral: - `\tConsider upgrading your local version to 18.\n` + `\tYour project is being built for Node.js ${major} as the runtime.\n` + + `\tThis version is deprecated by Vercel Serverless Functions, and scheduled to be disabled on ${removeDate}.\n` + + // biome-ignore lint/style/noUnusedTemplateLiteral: + `\tConsider upgrading your local version to 18.\n` ); return `nodejs${major}.x`; } From 8f73f6d21c448bd37e7eb236abe7117263d7821f Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 11 Sep 2024 14:19:21 +0200 Subject: [PATCH 2/2] feat: make sure polyfill runs first --- packages/netlify/src/polyfill.ts | 3 +++ packages/netlify/src/ssr-function.ts | 5 +++-- packages/node/src/polyfill.ts | 3 +++ packages/node/src/server.ts | 7 ++++--- packages/vercel/src/serverless/entrypoint.ts | 7 ++++--- packages/vercel/src/serverless/polyfill.ts | 3 +++ 6 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 packages/netlify/src/polyfill.ts create mode 100644 packages/node/src/polyfill.ts create mode 100644 packages/vercel/src/serverless/polyfill.ts diff --git a/packages/netlify/src/polyfill.ts b/packages/netlify/src/polyfill.ts new file mode 100644 index 000000000..dc00f45d7 --- /dev/null +++ b/packages/netlify/src/polyfill.ts @@ -0,0 +1,3 @@ +import { applyPolyfills } from 'astro/app/node'; + +applyPolyfills(); diff --git a/packages/netlify/src/ssr-function.ts b/packages/netlify/src/ssr-function.ts index c8502cbe7..5ea2e97f1 100644 --- a/packages/netlify/src/ssr-function.ts +++ b/packages/netlify/src/ssr-function.ts @@ -1,10 +1,11 @@ +// Keep at the top +import './polyfill.js'; + import type { Context } from '@netlify/functions'; import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; -import { applyPolyfills } from 'astro/app/node'; import { setGetEnv } from 'astro/env/setup'; -applyPolyfills(); setGetEnv((key) => process.env[key]); export interface Args { diff --git a/packages/node/src/polyfill.ts b/packages/node/src/polyfill.ts new file mode 100644 index 000000000..dc00f45d7 --- /dev/null +++ b/packages/node/src/polyfill.ts @@ -0,0 +1,3 @@ +import { applyPolyfills } from 'astro/app/node'; + +applyPolyfills(); diff --git a/packages/node/src/server.ts b/packages/node/src/server.ts index 93d75d360..cef262b47 100644 --- a/packages/node/src/server.ts +++ b/packages/node/src/server.ts @@ -1,13 +1,14 @@ +// Keep at the top +import './polyfill.js'; + import type { SSRManifest } from 'astro'; -import { NodeApp, applyPolyfills } from 'astro/app/node'; +import { NodeApp } from 'astro/app/node'; import { setGetEnv } from 'astro/env/setup'; import createMiddleware from './middleware.js'; import { createStandaloneHandler } from './standalone.js'; import startServer from './standalone.js'; import type { Options } from './types.js'; -// This needs to run first because some internals depend on `crypto` -applyPolyfills(); setGetEnv((key) => process.env[key]); export function createExports(manifest: SSRManifest, options: Options) { diff --git a/packages/vercel/src/serverless/entrypoint.ts b/packages/vercel/src/serverless/entrypoint.ts index e2c146ed8..01279a2b4 100644 --- a/packages/vercel/src/serverless/entrypoint.ts +++ b/packages/vercel/src/serverless/entrypoint.ts @@ -1,6 +1,9 @@ +// Keep at the top +import './polyfill.js'; + import type { IncomingMessage, ServerResponse } from 'node:http'; import type { SSRManifest } from 'astro'; -import { NodeApp, applyPolyfills } from 'astro/app/node'; +import { NodeApp } from 'astro/app/node'; import { setGetEnv } from 'astro/env/setup'; import { ASTRO_LOCALS_HEADER, @@ -9,8 +12,6 @@ import { ASTRO_PATH_PARAM, } from './adapter.js'; -// Run polyfills immediately so any dependent code can use the globals -applyPolyfills(); setGetEnv((key) => process.env[key]); export const createExports = ( diff --git a/packages/vercel/src/serverless/polyfill.ts b/packages/vercel/src/serverless/polyfill.ts new file mode 100644 index 000000000..dc00f45d7 --- /dev/null +++ b/packages/vercel/src/serverless/polyfill.ts @@ -0,0 +1,3 @@ +import { applyPolyfills } from 'astro/app/node'; + +applyPolyfills();