Skip to content

Commit

Permalink
chore: port adapter related changes from astro #11864
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Sep 4, 2024
1 parent 17a7fe5 commit 7cd4ad0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-jobs-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': major
---

Updates the adapter to use new `IntegrationRouteData` type
8 changes: 4 additions & 4 deletions packages/vercel/src/lib/redirects.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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: <explanation>
let redirects: VercelRoute[] = [];

Expand Down
77 changes: 39 additions & 38 deletions packages/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
AstroConfig,
AstroIntegration,
AstroIntegrationLogger,
RouteData,
IntegrationRouteData,
} from 'astro';
import { AstroError } from 'astro/errors';
import glob from 'fast-glob';
Expand Down Expand Up @@ -195,7 +195,7 @@ export default function vercelServerless({
let _config: AstroConfig;
let _buildTempFolder: URL;
let _serverEntry: string;
let _entryPoints: Map<RouteData, URL>;
let _entryPoints: Map<IntegrationRouteData, URL>;
let _middlewareEntryPoint: URL | undefined;
// Extra files to be merged with `includeFiles` during build
const extraFilesToInclude: URL[] = [];
Expand Down Expand Up @@ -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: <explanation>
`\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: <explanation>
`\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',
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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),
}
: {}),
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -531,11 +532,11 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru
// biome-ignore lint/style/useTemplate: <explanation>
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\n` +
`\tThe local Node.js version (${major}) is not supported by Vercel Serverless Functions.\n` +
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\tYour project will use Node.js 18 as the runtime instead.\n` +
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\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: <explanation>
`\tYour project will use Node.js 18 as the runtime instead.\n` +
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\tConsider switching your local version to 18.\n`
);
return 'nodejs18.x';
}
Expand Down Expand Up @@ -564,10 +565,10 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru
// biome-ignore lint/style/useTemplate: <explanation>
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\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: <explanation>
`\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: <explanation>
`\tConsider upgrading your local version to 18.\n`
);
return `nodejs${major}.x`;
}
Expand Down

0 comments on commit 7cd4ad0

Please sign in to comment.