aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/vercel/src/serverless/adapter.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/vercel/src/serverless/adapter.ts')
-rw-r--r--packages/integrations/vercel/src/serverless/adapter.ts118
1 files changed, 68 insertions, 50 deletions
diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts
index 364d2c861..1d7e64de4 100644
--- a/packages/integrations/vercel/src/serverless/adapter.ts
+++ b/packages/integrations/vercel/src/serverless/adapter.ts
@@ -194,9 +194,11 @@ export default function vercelServerless({
}: VercelServerlessConfig = {}): AstroIntegration {
if (maxDuration) {
if (typeof maxDuration !== 'number') {
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
throw new TypeError(`maxDuration must be a number`, { cause: maxDuration });
}
if (maxDuration <= 0) {
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
throw new TypeError(`maxDuration must be a positive number`, { cause: maxDuration });
}
}
@@ -217,10 +219,11 @@ export default function vercelServerless({
'astro:config:setup': async ({ command, config, updateConfig, injectScript, logger }) => {
if (maxDuration && maxDuration > 900) {
logger.warn(
- `maxDuration is set to ${maxDuration} seconds, which is longer than the maximum allowed duration of 900 seconds.`,
+ `maxDuration is set to ${maxDuration} seconds, which is longer than the maximum allowed duration of 900 seconds.`
);
logger.warn(
- `Please make sure that your plan allows for this duration. See https://vercel.com/docs/functions/serverless-functions/runtimes#maxduration for more information.`,
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
+ `Please make sure that your plan allows for this duration. See https://vercel.com/docs/functions/serverless-functions/runtimes#maxduration for more information.`
);
}
@@ -229,7 +232,7 @@ export default function vercelServerless({
'head-inline',
await getInjectableWebAnalyticsContent({
mode: command === 'dev' ? 'development' : 'production',
- }),
+ })
);
}
if (command === 'build' && speedInsights?.enabled) {
@@ -243,9 +246,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` +
- `\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',
@@ -277,27 +281,32 @@ export default function vercelServerless({
imagesConfig,
command,
devImageService,
- config.image,
+ config.image
),
});
},
'astro:config:done': ({ setAdapter, config, logger }) => {
if (functionPerRoute === true) {
logger.warn(
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\n` +
- `\tVercel's hosting plans might have limits to the number of functions you can create.\n` +
- `\tMake sure to check your plan carefully to avoid incurring additional costs.\n` +
- `\tYou can set functionPerRoute: false to prevent surpassing the limit.\n`,
+ `\tVercel's hosting plans might have limits to the number of functions you can create.\n` +
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
+ `\tMake sure to check your plan carefully to avoid incurring additional costs.\n` +
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
+ `\tYou can set functionPerRoute: false to prevent surpassing the limit.\n`
);
logger.warn(
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
`\n` +
- `\t\`functionPerRoute\` is deprecated and will be removed in a future version of the adapter.\n`,
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
+ `\t\`functionPerRoute\` is deprecated and will be removed in a future version of the adapter.\n`
);
}
setAdapter(
- getAdapter({ functionPerRoute, edgeMiddleware, middlewareSecret, skewProtection }),
+ getAdapter({ functionPerRoute, edgeMiddleware, middlewareSecret, skewProtection })
);
_config = config;
@@ -306,13 +315,13 @@ export default function vercelServerless({
if (config.output === 'static') {
throw new AstroError(
- '`output: "server"` or `output: "hybrid"` is required to use the serverless adapter.',
+ '`output: "server"` or `output: "hybrid"` is required to use the serverless adapter.'
);
}
},
'astro:build:ssr': async ({ entryPoints, middlewareEntryPoint }) => {
_entryPoints = new Map(
- Array.from(entryPoints).filter(([routeData]) => !routeData.prerender),
+ Array.from(entryPoints).filter(([routeData]) => !routeData.prerender)
);
_middlewareEntryPoint = middlewareEntryPoint;
},
@@ -397,12 +406,13 @@ export default function vercelServerless({
await builder.buildMiddlewareFolder(
_middlewareEntryPoint,
MIDDLEWARE_PATH,
- middlewareSecret,
+ middlewareSecret
);
}
const fourOhFourRoute = routes.find((route) => route.pathname === '/404');
// Output configuration
// https://vercel.com/docs/build-output-api/v3#build-output-configuration
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
await writeJson(new URL(`./config.json`, _config.outDir), {
version: 3,
routes: [
@@ -416,31 +426,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),
+ }
: {}),
});
@@ -472,8 +482,8 @@ class VercelBuilder {
readonly includeFiles: URL[],
readonly logger: AstroIntegrationLogger,
readonly maxDuration?: number,
- readonly runtime = getRuntime(process, logger),
- ) {}
+ readonly runtime = getRuntime(process, logger)
+ ) { }
async buildServerlessFolder(entry: URL, functionName: string) {
const { config, includeFiles, excludeFiles, logger, NTF_CACHE, runtime, maxDuration } = this;
@@ -491,7 +501,7 @@ class VercelBuilder {
excludeFiles,
logger,
},
- NTF_CACHE,
+ NTF_CACHE
);
// Enable ESM
@@ -513,7 +523,7 @@ class VercelBuilder {
await this.buildServerlessFolder(entry, functionName);
const prerenderConfig = new URL(
`./functions/${functionName}.prerender-config.json`,
- this.config.outDir,
+ this.config.outDir
);
// https://vercel.com/docs/build-output-api/v3/primitives#prerender-configuration-file
await writeJson(prerenderConfig, {
@@ -533,9 +543,10 @@ class VercelBuilder {
new URL(VERCEL_EDGE_MIDDLEWARE_FILE, this.config.srcDir),
new URL('./middleware.mjs', functionFolder),
middlewareSecret,
- this.logger,
+ this.logger
);
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
await writeJson(new URL(`./.vc-config.json`, functionFolder), {
runtime: 'edge',
entrypoint: 'middleware.mjs',
@@ -549,10 +560,14 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru
const support = SUPPORTED_NODE_VERSIONS[major];
if (support === undefined) {
logger.warn(
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
+ // biome-ignore lint/style/useTemplate: <explanation>
`\n` +
- `\tThe local Node.js version (${major}) is not supported by Vercel Serverless Functions.\n` +
- `\tYour project will use Node.js 18 as the runtime instead.\n` +
- `\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';
}
@@ -562,26 +577,29 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru
if (support.status === 'retiring') {
if (support.warnDate && new Date() >= support.warnDate) {
logger.warn(
- `Your project is being built for Node.js ${major} as the runtime, which is retiring by ${support.removal}.`,
+ `Your project is being built for Node.js ${major} as the runtime, which is retiring by ${support.removal}.`
);
}
return `nodejs${major}.x`;
}
if (support.status === 'beta') {
logger.warn(
- `Your project is being built for Node.js ${major} as the runtime, which is currently in beta for Vercel Serverless Functions.`,
+ `Your project is being built for Node.js ${major} as the runtime, which is currently in beta for Vercel Serverless Functions.`
);
return `nodejs${major}.x`;
}
if (support.status === 'deprecated') {
const removeDate = new Intl.DateTimeFormat(undefined, { dateStyle: 'long' }).format(
- support.removal,
+ support.removal
);
logger.warn(
+ // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
+ // biome-ignore lint/style/useTemplate: <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` +
- `\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`;
}