Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional Route Segments #162

Open
sandstrom opened this issue Dec 18, 2020 · 0 comments
Open

Optional Route Segments #162

sandstrom opened this issue Dec 18, 2020 · 0 comments

Comments

@sandstrom
Copy link

sandstrom commented Dec 18, 2020

Problem

For localization it's common with an URL structure like this:

/
/about/
/pricing/
…

/de/
/de/about/
/de/pricing/
…

/es/
/es/about/
/es/pricing/
…

Optional segments like this doesn't seem to be supported by the route recognizer, making it difficult to use Ember for any localized website.

Desired outcome

router.add([
  { path: "/:locale/", handler: mother },
]);

let result1 = router.recognize("/es/about");
result1 === [
  { handler: mother, params: { locale: 'es' } },
  { handler: about, params: {} }
];

let result2 = router.recognize("/about");
result2 === [
  { handler: mother, params: { locale: null } },
  { handler: about, params: {} }
];

Workarounds

For Ember apps, the specific case of a language prefix can be solved by using a custom location class, that will extract/inject any language prefix before passing the URL along to the underlying location implementation.

https://api.emberjs.com/ember/release/classes/Location

Prior Art

Related RFCs

This has been proposed in emberjs/rfcs#154, though that proposal include other changes too. Also, I'm unsure about whether this should go into the route recognizer (I think so) or in software consuming this library (not so sure).

Relevant and semi-relevant threads online

https://stackoverflow.com/questions/26871152/ember-optional-url-segments
https://stackoverflow.com/questions/44272568/how-to-make-route-parameters-optional-in-ember-js
https://discuss.emberjs.com/t/dynamic-segments-optional/15699
https://stackoverflow.com/questions/19991135/ember-js-define-route-with-optional-parameter-and-default-model/27187769
https://discuss.emberjs.com/t/is-there-really-no-way-to-make-an-optional-dynamic-segment-in-ember/5010/8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant