sveltejs / sveltejs/kit

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

Open
#9,032 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pkg:adapter-vercel
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how Vercel build output generates per-prerendered-page redirects and overrides, using the route examples in src/routes/[a=b]/+page.svelte and src/routes/[c]/+page.svelte as edge cases. Investigate why the first redirect is needed for trailingSlash: 'never'. Done means generating per-SvelteKit-route redirect pairs while preserving distinct trailingSlash settings and correct matching for prerendered routes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.