Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vercel): trailing slash conflict #10082

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading