Skip to content

Instantly share code, notes, and snippets.

@AndrewIngram
Last active June 19, 2019 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AndrewIngram/8d4c4ccd9bd10415a375caacade9f5ca to your computer and use it in GitHub Desktop.
Save AndrewIngram/8d4c4ccd9bd10415a375caacade9f5ca to your computer and use it in GitHub Desktop.
Route requirements

I have a bunch of routes:

/gb/

-> name=homepage, page=HomepageGb, locale=en_gb

/es/

-> name=homepage, page=HomepageEs, locale=es_es

/gb/about-us/

-> name=about, page=About, locale=en_gb

/es/sobre-nosotros/

-> name=about, page=About, locale=es_es

/gb/scooter/

-> name=product, page=ScooterGb, code=scooter, locale=en_gb

/gb/:code/

-> name=product, page=GenericProduct, code, locale=en_gb

/gb/promotions/:code/

- name=promotion, page=Promotion, code, locale=en_gb

/some-special-promotion/

- name=promotion, page=Promotion, code=some-special-promotion, locale=en_gb

A few key things I can do:

  1. I can iterate over products that come back from our API, and construct links based on the route "name", eg "product" and the product's code, eg "scooter". The fact that some products have completely different page files and URL mount points is irrelevant, I just call reverse('product', {code: 'scooter', locale: 'en_gb'}) and get the correct URL. This seems to be hard to do with file-system routes.

  2. I can have have a menu component that can be shared between each locale, that renders links to the homepage and about page. If I render within the context of the en_gb locale, I get the GB links, if I render in es_es_ I get the Spain links. Note above that we've even translated the path of the "about" page for Spain. I just call reverse('about', {locale: 'en_gb'}) or reverse('about', {locale: 'es_es'}) and get the correct page. Again, this is hard to do with file-system routes.

  3. For some promotions I can completely override its URL on the website, without anything else having to change (such as a component that renders a link to a promotion). This does actually happen because some businesses we partner with love their vanity URLs.

Centralised route config has its downsides (mainly in terms of file-size if it grows beyond a few dozen routes), but it also unlocks some powerful use cases. As far as I've been able to see from various examples of file-system routing (in dynamic sites, static is different), not all of these use cases can be satisfied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment