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 redirects with trailingSlash always #7447

Merged
merged 1 commit into from
Jun 22, 2023
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/nervous-paws-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Fix redirects for root page when using `trailingSlash: "always"`
2 changes: 1 addition & 1 deletion packages/integrations/vercel/src/lib/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function getRedirects(routes: RouteData[], config: AstroConfig): VercelRo
headers: { Location: getRedirectLocation(route, config) },
status: getRedirectStatus(route),
});
} else if (route.type === 'page') {
} else if (route.type === 'page' && route.route !== '/') {
if (config.trailingSlash === 'always') {
redirects.push({
src: config.base + getMatchPattern(route.segments),
Expand Down
15 changes: 14 additions & 1 deletion packages/integrations/vercel/test/redirects.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Redirects', () => {
Expand All @@ -18,6 +17,7 @@ describe('Redirects', () => {
},
'/blog/[...slug]': '/team/articles/[...slug]',
},
trailingSlash: 'always',
experimental: {
redirects: true,
},
Expand Down Expand Up @@ -56,4 +56,17 @@ describe('Redirects', () => {
expect(blogRoute.headers.Location.startsWith('/team/articles')).to.equal(true);
expect(blogRoute.status).to.equal(301);
});

it('define trailingSlash redirect for sub pages', async () => {
const config = await getConfig();

const subpathRoute = config.routes.find((r) => r.src === '/\\/subpage');
expect(subpathRoute).to.not.be.undefined;
expect(subpathRoute.headers.Location).to.equal('/subpage/');
});

it('does not define trailingSlash redirect for root page', async () => {
const config = await getConfig();
expect(config.routes.find((r) => r.src === '/')).to.be.undefined;
});
});