sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.77k stars 1.96k forks source link

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

Open rinart73 opened 1 year ago

rinart73 commented 1 year ago

Describe the problem

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

src/params/news.ts:

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

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

Expected behaviour:

Actual behaviour:

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 ).

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

Conduitry commented 1 year ago

Does checking the rest param in your load doing a throw error(404, 'whatever') if it's zero-length work?

rinart73 commented 1 year ago

Not really, because it would use +layout.svelte and +error.svelte from the routes/[...news=news]/[slug]/ directory instead of the expected routes/. So essentially it would be treated like a missing news post instead of a missing top level page.

Even then, I think rest parameters aren't very good choice for my specific task since they I won't be able to track nested translateable URLs like /locale/translate1/translate2. I guess focusing on https://github.com/sveltejs/kit/issues/4500 would be better.