sveltejs / sveltejs/kit

Allow rest parameters to be forced to be non-zero length

Open
#10,305 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the problem

I'm trying to make a multilingual website with localized URLs.
I have been advised to use the following structure:

  • [...news=news]
    • [slug]

src/params/news.ts:

import type { ParamMatcher } from '@sveltejs/kit';

export const match: ParamMatcher = (param) => {
    return param === 'news' || param === 'ru/novosti';
};

Expected behaviour:

  • /news - News list page in English
  • /news/foo - "Foo" news post in English from DB
  • /ru/novosti - News list page in Russian
  • /ru/novosti/foo - "Foo" news post in Russian from DB
  • /ru/news - 404
  • /novosti - 404
  • /barbaz - 404

Actual behaviour:

  • /novosti - "Novosti" news post in English
  • /barbaz - "Barbaz" news post in English

If I understand correctly this happens because in the past rest parameters were changed to allow zero-length ( https://github.com/sveltejs/kit/issues/554 ), in which case a corresponding matcher doesn't run on them.

Describe the proposed solution

Maybe allow to specify which rest parameters can have zero-length and which can't?

Alternatives considered

Initially I thought of a simpler solution, but it's currently not supported because there is no way to read already matched "parent" paramerers ( https://github.com/sveltejs/kit/issues/4500 ).

  • [lang=lang]
    • [news=news]
      • [slug]

If everything else fails I guess I'll have to abandon the idea of localizing URLs, even though it's nice for usability.

Importance

would be nice to have

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 with the localized route and matcher example in src/params/news.ts, then trace how the rest parameter handles empty and non-empty paths. Define the configuration needed to require a non-empty rest parameter and verify the expected localized and 404 routes from the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.