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

Routing too many redirects when using the config.json file generated by the Vercel adapter #418

Open
1 task
yuhangch opened this issue Oct 9, 2024 · 9 comments
Labels
needs triage Issue needs to be triaged

Comments

@yuhangch
Copy link

yuhangch commented Oct 9, 2024

Astro Info

Astro                    v4.15.12
Node                     v18.20.4
System                   Windows (x64)
Package Manager          unknown
Output                   server
Adapter                  @astrojs/vercel/serverless
Integrations             unocss
                         astro-expressive-code

Describe the Bug

Configuration:

  1. Set trailingSlash: 'always' in astro.config.mjs
  2. Set build: { format: 'directory' }

Problem:
When visiting the /docs/ route, I encounter a "Redirected you too many times" error.

Details:

  • The directory structure is as follows:

    pages
    ├───docs
    │   ├───index.astro
    │   ├───[...slug].astro
    
  • The config.json file generated by the adapter looks like this:

    {
      "version": 3,
      "routes": [
        {
          "src": "/docs",
          "headers": {
            "Location": "/docs/"
          },
          "status": 308
        },
        {
          "src": "/docs/(?:\\/(.*?))?",
          "headers": {
            "Location": "/docs/$1/"
          },
          "status": 308
        }
      ]
    }

Issue:

  • The /docs/ route appears to redirect indefinitely, It seems the redirection rules are creating a loop for this route.

When delete trailingSlash: 'always' in astro.config.mjs, the redirect in config.json was not be generated, and /docs/ works well.

What's the expected result?

The /docs/ route should resolve correctly without an infinite redirect loop with trailingSlash: 'always' config.

Link to Minimal Reproducible Example

http://yuhang-ch-git-loop-redirect-yuhang-chens-projects.vercel.app/docs/

Participation

  • I am willing to submit a pull request for this issue.
@ematipico ematipico added the needs repro Issue needs a reproduction label Oct 9, 2024
@ematipico
Copy link
Member

@yuhangch the link you provided isn't a valid reproduction

@yuhangch
Copy link
Author

yuhangch commented Oct 9, 2024

Sorry, I just disable the Vercel Authentication

There is a capture:

20241009_193142.mp4

@ematipico
Copy link
Member

@yuhangch we need a reproduction which we can debug. Please the stackblitz URL provided

@yuhangch
Copy link
Author

Okay got it. I can't completely reproduce the issue on Stackblitz because the behavior is different on the dev server compared to when deployed with the Vercel adapter. Below is the simplest code. I have still deployed it to a sample project on Vercel.


@ematipico
Copy link
Member

It's okay, we can deploy it ourselves on Vercel

@ematipico ematipico added needs triage Issue needs to be triaged and removed needs repro Issue needs a reproduction labels Oct 10, 2024
@bgentry
Copy link

bgentry commented Oct 25, 2024

I believe I just encountered this same issue. The regression appears to be introduced as of @astrojs/vercel v7.8.2.

I have a route served by a rest param for an SSR page at /path/[...slug].astro. This route is rendering some external content which is prerendered with relative links, so to make that work correctly I'm using trailing slashes only on this path prefix via redirects within the page's JS:

---
import PackagePageLayout from '@/layouts/pkg/PackagePageLayout.astro';
import { getEntry } from 'astro:content';

const { slug } = Astro.params;
if (!slug) {
  return new Response('Not found', { status: 404 });
}

const splitSlug = slug.split('/');
if (!slug.endsWith('/')) {
  // Permanent redirect to the canonical URL with trailing slash:
  const url = new URL(Astro.request.url);
  url.pathname = `${url.pathname}/`;
  return Astro.redirect(url, 301);
}

// fetch content and render ...
---
<html />

With the @astrojs/vercel adapter v7.8.1, this scheme works just fine. Paths under this prefix are redirected to have a trailing slash, both locally and on Vercel. However, as soon as I upgrade to v7.8.2, this no longer works and I instead see an infinite redirect loop between the paths with and without the trailing slash. I'm specifically getting a 308 Permanent Redirect from the trailing slash path back to the one without.

Locally, this still works fine, it's only once deploying to vercel that it breaks.

@artt
Copy link

artt commented Dec 7, 2024

Just bumped into this as well. Created a reproducible repo here if it helps: https://github.com/artt/trailingslash-vercel

@yuhangch @bgentry did you find any workaround?

@bgentry
Copy link

bgentry commented Dec 7, 2024

No, my workaround has been to pin the version from before the breakage and hope somebody takes action on this reported issue 😂🤞🏻

@artt
Copy link

artt commented Dec 8, 2024

For some reason 7.8.1 still doesn't work for me. https://github.com/artt/trailingslash-vercel/tree/v7.8.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

4 participants