diff --git a/.changeset/nervous-paws-knock.md b/.changeset/nervous-paws-knock.md new file mode 100644 index 000000000000..4051bd8c2c1d --- /dev/null +++ b/.changeset/nervous-paws-knock.md @@ -0,0 +1,5 @@ +--- +'@astrojs/vercel': patch +--- + +Fix redirects for root page when using `trailingSlash: "always"` diff --git a/packages/integrations/vercel/src/lib/redirects.ts b/packages/integrations/vercel/src/lib/redirects.ts index 5c6bb605b57c..8613be2ed72d 100644 --- a/packages/integrations/vercel/src/lib/redirects.ts +++ b/packages/integrations/vercel/src/lib/redirects.ts @@ -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), diff --git a/packages/integrations/vercel/test/fixtures/redirects/src/pages/subpage.astro b/packages/integrations/vercel/test/fixtures/redirects/src/pages/subpage.astro new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/integrations/vercel/test/redirects.test.js b/packages/integrations/vercel/test/redirects.test.js index 16d845b044f8..35bbfcd519a2 100644 --- a/packages/integrations/vercel/test/redirects.test.js +++ b/packages/integrations/vercel/test/redirects.test.js @@ -1,5 +1,4 @@ import { expect } from 'chai'; -import * as cheerio from 'cheerio'; import { loadFixture } from './test-utils.js'; describe('Redirects', () => { @@ -18,6 +17,7 @@ describe('Redirects', () => { }, '/blog/[...slug]': '/team/articles/[...slug]', }, + trailingSlash: 'always', experimental: { redirects: true, }, @@ -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; + }); });