kit icon indicating copy to clipboard operation
kit copied to clipboard

Generate per-route redirects in Vercel build output instead of per-page

Open Rich-Harris opened this issue 2 years ago • 3 comments

Describe the problem

In order to get the correct trailing slash redirect behaviour for prerendered pages on Vercel, it's necessary to add an override plus two routes for each page:

{
  "version": 3,
  "routes": [
    // ...
    {
      "src": "/docs/introduction",
      "dest": "/docs/introduction/"
    },
    {
      "src": "/docs/introduction/",
      "status": 308,
      "headers": {
        "Location": "/docs/introduction"
      }
    },
    {
      "src": "/docs/configuration",
      "dest": "/docs/configuration/"
    },
    {
      "src": "/docs/configuration/",
      "status": 308,
      "headers": {
        "Location": "/docs/configuration"
      }
    },
    // ...
  ],
  "overrides": {
    // ...
    "docs/introduction.html": {
      "path": "docs/introduction"
    },
    "docs/configuration.html": {
      "path": "docs/configuration"
    },
    // ...
  }
}

Since there's a limit to how many routes you can have in a project, this means that there's also a (quite low) limit on the number of prerendered pages.

Describe the proposed solution

We should be able to create a pair of Vercel redirect routes for each SvelteKit route, instead of for each prerendered page:

{
  "version": 3,
  "routes": [
    // ...
    {
      "src": "/docs/([^/]+)",
      "dest": "/docs/$1/"
    },
    {
      "src": "/docs/([^/]+)/",
      "status": 308,
      "headers": {
        "Location": "/docs/$1"
      }
    },
    // ...
  ],
  "overrides": {
    // ...
    "docs/introduction.html": {
      "path": "docs/introduction"
    },
    "docs/configuration.html": {
      "path": "docs/configuration"
    },
    // ...
  }
}

(I actually can't recall why the first of those is necessary in the trailingSlash: 'never' case, need to investigate.)

The challenge will be to do this while respecting edge cases like this...

src/routes/[a=b]/+page.svelte
src/routes/[c]/+page.svelte

...where the two routes have different trailingSlash config and /[a=b] is a prerendered route that shouldn't prevent /[c] from matching.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Rich-Harris avatar Feb 13 '23 19:02 Rich-Harris

How long can a src regex become? We could do /path/(possibility1|possibility2|..)

{
  "version": 3,
  "routes": [
    // ...
    {
      "src": "/docs/(introduction|configuration)",
      "dest": "/docs/$1/"
    },
    {
      "src": "/docs/(introduction|configuration)/",
      "status": 308,
      "headers": {
        "Location": "/docs/$1"
      }
    },
    // ...
  ],
  "overrides": {
    // ...
    "docs/introduction.html": {
      "path": "docs/introduction"
    },
    "docs/configuration.html": {
      "path": "docs/configuration"
    },
    // ...
  }
}

Not sure if this has any implications on performance when the regex becomes too big or if there are nested possibilities.

dummdidumm avatar Apr 21 '23 13:04 dummdidumm

I just ran into this while deploying a SvelteKit site with ~550 prerendered pages. I tried setting trailingSlash to ignore but it still generates the same number of routes and overrides in config.json as the default never. Is there a workaround for this if I don't care about trailing slashes? (I'm using canonical links on those pages so SEO isn't an issue.)


The full error, so people can find this issue via search:

Build Failed
Maximum number of routes (rewrites, redirects, etc) exceeded. Max is 1024, received 1131. Please reduce the number of routes.

utkarshkukreti avatar Apr 25 '23 06:04 utkarshkukreti

Facing the same issue. Does this mean that we basically can't deploy projects with a large number of pre-rendered pages to Vercel? Like the previous commenter, it looks like setting 'trailingSlash' doesn't really do anything and you always end up with a very large config.json file with all the redirects..

jormaj avatar May 27 '23 19:05 jormaj

I ran into this issue as well. Is there a known workaround?

turnabout avatar Mar 10 '24 21:03 turnabout