From b2406d07d9dd3ec727014396a774f5b4f651d072 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Wed, 4 Sep 2024 19:47:19 +0200 Subject: [PATCH] chore: port adapter related changes from astro #11864 --- packages/vercel/src/lib/redirects.ts | 8 +-- packages/vercel/src/serverless/adapter.ts | 77 ++++++++++++----------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/packages/vercel/src/lib/redirects.ts b/packages/vercel/src/lib/redirects.ts index 39cbfa0fe..1d7d4eca5 100644 --- a/packages/vercel/src/lib/redirects.ts +++ b/packages/vercel/src/lib/redirects.ts @@ -1,6 +1,6 @@ import nodePath from 'node:path'; import { appendForwardSlash, removeLeadingForwardSlash } from '@astrojs/internal-helpers/path'; -import type { AstroConfig, RouteData, RoutePart } from 'astro'; +import type { AstroConfig, IntegrationRouteData, RoutePart } from 'astro'; const pathJoin = nodePath.posix.join; @@ -85,7 +85,7 @@ function getReplacePattern(segments: RoutePart[][]) { return result; } -function getRedirectLocation(route: RouteData, config: AstroConfig): string { +function getRedirectLocation(route: IntegrationRouteData, config: AstroConfig): string { if (route.redirectRoute) { const pattern = getReplacePattern(route.redirectRoute.segments); const path = config.trailingSlash === 'always' ? appendForwardSlash(pattern) : pattern; @@ -99,7 +99,7 @@ function getRedirectLocation(route: RouteData, config: AstroConfig): string { } } -function getRedirectStatus(route: RouteData): number { +function getRedirectStatus(route: IntegrationRouteData): number { if (typeof route.redirect === 'object') { return route.redirect.status; } @@ -116,7 +116,7 @@ export function escapeRegex(content: string) { return `^/${getMatchPattern(segments)}$`; } -export function getRedirects(routes: RouteData[], config: AstroConfig): VercelRoute[] { +export function getRedirects(routes: IntegrationRouteData[], config: AstroConfig): VercelRoute[] { // biome-ignore lint/style/useConst: let redirects: VercelRoute[] = []; diff --git a/packages/vercel/src/serverless/adapter.ts b/packages/vercel/src/serverless/adapter.ts index a762103f9..313f50058 100644 --- a/packages/vercel/src/serverless/adapter.ts +++ b/packages/vercel/src/serverless/adapter.ts @@ -7,7 +7,7 @@ import type { AstroConfig, AstroIntegration, AstroIntegrationLogger, - RouteData, + IntegrationRouteData, } from 'astro'; import { AstroError } from 'astro/errors'; import glob from 'fast-glob'; @@ -195,7 +195,7 @@ export default function vercelServerless({ let _config: AstroConfig; let _buildTempFolder: URL; let _serverEntry: string; - let _entryPoints: Map; + let _entryPoints: Map; let _middlewareEntryPoint: URL | undefined; // Extra files to be merged with `includeFiles` during build const extraFilesToInclude: URL[] = []; @@ -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', @@ -325,7 +325,8 @@ export default function vercelServerless({ // Multiple entrypoint support if (_entryPoints.size) { - const getRouteFuncName = (route: RouteData) => route.component.replace('src/pages/', ''); + const getRouteFuncName = (route: IntegrationRouteData) => + route.component.replace('src/pages/', ''); const getFallbackFuncName = (entryFile: URL) => basename(entryFile.toString()) @@ -394,31 +395,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), + } : {}), }); @@ -451,7 +452,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 +532,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 +565,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`; }