Skip to content

Commit

Permalink
fix(vercel): trailing slash conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre committed Feb 10, 2024
1 parent 8f37cf7 commit af294fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-pets-dream.md
Original file line number Diff line number Diff line change
@@ -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`
21 changes: 21 additions & 0 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit af294fd

Please sign in to comment.