From 2ffc5721bc22631c44d90ac43ec27fdb0b5b2d1b Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Tue, 13 Feb 2024 12:36:22 +0100 Subject: [PATCH] fix(vercel): trailing slash conflict (#10082) --- .changeset/breezy-pets-dream.md | 5 +++++ .../vercel/src/serverless/adapter.ts | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 .changeset/breezy-pets-dream.md diff --git a/.changeset/breezy-pets-dream.md b/.changeset/breezy-pets-dream.md new file mode 100644 index 000000000000..45eed91cf905 --- /dev/null +++ b/.changeset/breezy-pets-dream.md @@ -0,0 +1,5 @@ +--- +"@astrojs/vercel": patch +--- + +Prevents infinite redirects when Astro `trailingSlash` configuration is set to `"always"` and "vercel.json" `trailingSlash` configuration is set to `true` diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index 0281b62d4c0f..68897c6dc9b7 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -9,6 +9,7 @@ import { AstroError } from 'astro/errors'; import glob from 'fast-glob'; import { basename } from 'node:path'; import { pathToFileURL } from 'node:url'; +import { existsSync, readFileSync } from 'node:fs'; import { getAstroImageConfig, getDefaultImageConfig, @@ -222,6 +223,26 @@ export default function vercelServerless({ injectScript('page', 'import "@astrojs/vercel/speed-insights"'); } + const vercelConfigPath = new URL('vercel.json', config.root); + if (existsSync(vercelConfigPath)) { + try { + const vercelConfig = JSON.parse(readFileSync(vercelConfigPath, 'utf-8')); + 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` + ); + updateConfig({ + trailingSlash: 'ignore', + }); + } + } catch (_err) { + logger.warn(`Your "vercel.json" config is not a valid json file.`); + } + } + updateConfig({ outDir: new URL('./.vercel/output/', config.root), build: {