Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Infinite Redirect Loop with @astrojs/[email protected] When Using trailingSlash: 'always' #499

Closed
faiz9068 opened this issue Jan 9, 2025 · 2 comments · Fixed by #525
Closed

Comments

@faiz9068
Copy link

faiz9068 commented Jan 9, 2025

Astro Info

Astro                    v5.1.1
Node                     v20.14.0
System                   Windows (x64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/vercel
Integrations             @astrojs/tailwind
                         astro-lottie
                         @astrojs/mdx
                         @astrojs/svelte
                         @astrojs/react
                         @astrojs/sitemap
                         sitemap-copier

Describe the Bug

When deploying to Vercel with trailingSlash: "always", routes enter an infinite redirect loop. The issue occurs even when directly accessing URLs with trailing slashes. This appears to be a regression in the Vercel adapter's handling of trailing slashes.

Reproduction

The Vercel adapter generates overlapping redirect rules in config.json:

{
  "src": "/guides",
  "headers": {
    "Location": "/guides/"
  },
  "status": 308
},
{
  "src": "/guides(?:\\/(.*?))?",
  "headers": {
    "Location": "/guides/$1/"
  },
  "status": 308
}

The second pattern matches /guides/ and attempts to redirect to /guides//, creating an infinite loop.

Steps to Reproduce

  1. Create an Astro project with configurations:
    // astro.config.mjs
    export default defineConfig({
      trailingSlash: "always",
      output: "server",
      adapter: vercel()
    });
  2. Add a route at /guides/ (either static or dynamic)
  3. add /guides/[...slug] rest route
  4. Deploy to Vercel
  5. Visit /guides/

Expected Behavior

  • /guides should redirect to /guides/ (once)
  • /guides/ should render the page without redirecting

Actual Behavior

  • Accessing /guides/ results in "Too Many Redirects" error
  • Browser shows infinite loop between /guides/ and /guides//

Configuration

// astro.config.mjs
import { defineConfig } from "astro/config";
import tailwind from "@astrojs/tailwind";
import lottie from "astro-integration-lottie";
import mdx from "@astrojs/mdx";
import vercel from "@astrojs/vercel";
import svelte from "@astrojs/svelte";
import sitemap from "@astrojs/sitemap";
import react from "@astrojs/react";

export default defineConfig({
  trailingSlash: "always",
  integrations: [
    tailwind(),
    lottie(),
    mdx(),
    svelte(),
    react(),
    sitemap({...})
  ],
  output: "server",
  adapter: vercel()
});

Related Issues

Environment

  • Platform: Vercel
  • Browser: All browsers (Chrome, Firefox, Safari)
  • Operating System: All (not OS-specific)
@ascorbic
Copy link
Contributor

Can you try installing @astrojs/vercel@experimental--vercel-routing

@ascorbic
Copy link
Contributor

The fix is in @astrojs/[email protected]

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants