From af294fd03f23b725810dacedcc9e3ddeb495b8cf Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Sat, 10 Feb 2024 18:07:06 +0100 Subject: [PATCH] fix(vercel): trailing slash conflict --- .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: {