mercredi 17 octobre 2018

Redirect shorthand URLs with children to normal URL

I'm attempting to implement shorthand URLs which redirect to the regular URLs to make URLs easier and shorter to type in the address bar. I'm attempting to implement this for the crates.io website.

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/rand/... => /crates/rand/...

Here is a snippet of the relevant 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');
});

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




Aucun commentaire:

Enregistrer un commentaire