-
-
Notifications
You must be signed in to change notification settings - Fork 52
Routing too many redirects when using the config.json file generated by the Vercel adapter #418
Comments
@yuhangch the link you provided isn't a valid reproduction |
Sorry, I just disable the Vercel Authentication There is a capture: 20241009_193142.mp4 |
@yuhangch we need a reproduction which we can debug. Please the stackblitz URL provided |
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.
|
It's okay, we can deploy it ourselves on Vercel |
I believe I just encountered this same issue when using I have a route served by a rest param for an SSR page at ---
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 });
}
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 Locally, this still works fine, it's only once deploying to vercel that it breaks. |
Just bumped into this as well. Created a reproducible repo here if it helps: https://github.com/artt/trailingslash-vercel |
No, my workaround has been to pin the version from before the breakage and hope somebody takes action on this reported issue 😂🤞🏻 |
For some reason 7.8.1 still doesn't work for me. https://github.com/artt/trailingslash-vercel/tree/v7.8.1 |
For my specific issue, I have narrowed it down to #373 / 5b802a4 that introduced this breakage (cc @hrabiel). If I use patch-package to apply this diff to revert the meat of that commit, my 308 redirect issue goes away altogether. diff --git a/node_modules/@astrojs/vercel/dist/lib/redirects.js b/node_modules/@astrojs/vercel/dist/lib/redirects.js
index 47b2621..00d4857 100644
--- a/node_modules/@astrojs/vercel/dist/lib/redirects.js
+++ b/node_modules/@astrojs/vercel/dist/lib/redirects.js
@@ -28,31 +28,25 @@ function getParts(part, file) {
// 2022-04-26
function getMatchPattern(segments) {
return segments
- .map((segment, segmentIndex) => {
- return segment.length === 1 && segment[0].spread
- ? '(?:\\/(.*?))?'
- : // Omit leading slash if segment is a spread.
- // This is handled using a regex in Astro core.
- // To avoid complex data massaging, we handle in-place here.
- (segmentIndex === 0 ? '' : '/') +
- segment
- .map((part) => {
+ .map((segment) => {
+ return segment[0].spread
+ ? '(?:\\/(.*?))?'
+ : segment
+ .map(((part) => {
if (part)
- return part.spread
- ? '(.*?)'
- : part.dynamic
- ? '([^/]+?)'
- : part.content
- .normalize()
- .replace(/\?/g, '%3F')
- .replace(/#/g, '%23')
- .replace(/%5B/g, '[')
- .replace(/%5D/g, ']')
- .replace(/[*+?^${}()|[\]\\]/g, '\\$&');
- })
- .join('');
- })
- .join('');
+ return part.dynamic
+ ? '([^/]+?)'
+ : part.content
+ .normalize()
+ .replace(/\?/g, '%3F')
+ .replace(/#/g, '%23')
+ .replace(/%5B/g, '[')
+ .replace(/%5D/g, ']')
+ .replace(/[*+?^${}()|[\]\\]/g, '\\$&');
+ }))
+ .join('');
+ })
+ .join('/');
}
function getReplacePattern(segments) {
let n = 0; |
Can you install |
@ascorbic Just tried it, does not fix my issue. Hitting the path with the trailing slash gives me a 308 to the same route without the trailing slash. The content of this route with rest param is in my comment above: #418 (comment) Running locally without vercel, this works just fine and I'm allowed to render the routes with the trailing slash (despite
|
@bgentry could you share a minimal reproduction so I can take a look? |
@artt your repro seems to work for me with the experimental package: https://trailingslash-vercel-six.vercel.app/ |
@ascorbic sure, here's a simple repro created with these steps:
It's deployed on Vercel, try to visit Note that it appears step 4 seems to be a factor here. Without it, I get some redirect loop behaviors locally as well due to the redirect within my route. |
@bgentry that repro is using the published adapter version. Have you tried it with the experimental version that I shared above? |
@ascorbic sorry, wasn't clear which version you wanted on the repro. Yes I have tried the experimental version, it did not fix the issue on my real app and it doesn't fix it on the repro either. I pushed the updated version using the experimental |
@bgentry it's doing a redirect loop because you have I removed that and it's working fine: https://astro-vercel-redirect-loop-demo-ten.vercel.app/sub/foo |
The fix is in |
@ascorbic that is intentional. It works fine without the Vercel adapter and it worked fine with the adapter up until #373, which broke this behavior. The reason I need the trailing slash on these routes is that I’m serving up pre rendered HTML content with relative links. See the real deployment here: https://riverqueue.com/pkg/riverpro/latest/riverpro/ Again, this works fine everywhere (including locally) except when deployed with the Vercel adapter after #373 broke it. |
If you need the trailing slash, why are you setting |
@ascorbic the rest of my site relies on It seems that as of #373 @astrojs/vercel is using this setting to 308 redirect on all requests with a trailing slash, whether or not they are dynamically rendered and without giving the dynamic route an opportunity to decide what it wants to do. Again, this is different than what happened previously and it is not what Astro itself does—so it is a breaking change specific to the Vercel adapter. The version that's deployed live is using the latest |
This was a bug before (and is this bug is a side effect of it). It was supposed to work like it does currently. If you want it to not be "always" you can set it to "ignore" |
Astro Info
Describe the Bug
Configuration:
trailingSlash: 'always'
inastro.config.mjs
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:
The
config.json
file generated by the adapter looks like this:Issue:
/docs/
route appears to redirect indefinitely, It seems the redirection rules are creating a loop for this route.When delete
trailingSlash: 'always'
inastro.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 withtrailingSlash: 'always'
config.Link to Minimal Reproducible Example
http://yuhang-ch-git-loop-redirect-yuhang-chens-projects.vercel.app/docs/
Participation
The text was updated successfully, but these errors were encountered: