Redirect shorthand URLs with children to normal URL

99 Views Asked by At

I'm attempting to implement shorthand URLs which redirect to the regular URLs to make them more efficient to type in the address bar. I'm attempting to implement this for the crates.io project.

To clarify what I mean, I would like to redirect URLs like this:

  • /c => /crates
  • /c/rand => /crates/rand
  • /c/rand/0.5.5 => /crates/rand/0.5.5
  • /c/... => /crates/...

Here is a snippet of the relevant long routes as currently configured:

this.route('crates');
this.route('crate', { path: '/crates/:crate_id' }, function() {
    this.route('download');
    this.route('versions');
    this.route('version', { path: '/:version_num' });
    this.route('reverse-dependencies', { path: 'reverse_dependencies' });
    this.route('owners');
    this.route('docs');
    this.route('repo');
});

Here is what I've tried; I did attempt to implement these redirects by defining the following additional routes:

this.route('c');
this.route('c', { path: '/c/:crate_id' });

With this ./routes/c.js:

import Route from '@ember/routing/route';

export default Route.extend({
    afterModel(crate, path) {
        if (crate === undefined) this.transitionTo('crates');
        else this.transitionTo('crate', crate, path);
    },
});

This does only partially work. The /c and /c/rand routes are covered and work, while the others such as /c/rand/0.5.5 don't work.

I can implement all child route possibilities, but I don't feel that's the right way of doing so. I would like to prevent duplicate code.


How would I implement this properly? Is it possible to redirect anything starting with /c to their respective long routes? I've looked into the ember documentation, but didn't find the solution. I feel there must be a better way than what I've tried. Could anyone point me into the right direction?

0

There are 0 best solutions below