Search code examples
vue.jsnuxt.jsvue-router

Nuxt.js Root routing and root level slugs for multiple resources


I am building an e-commerce front-end using Nuxt and I would like to have root level slugs for as many resources as possible. The most important ones are Catalog Paths and Product Urls. An obvious was is to use the Nuxt file structure for routing to create:

  • .com/category/men/tshirt
  • .com/category/women/tshirt
  • .com/product/name-of-product-slug

What I want to achieve is:

  • .com/men/tshirt
  • .com/name-of-product-slug

The logic would be to check if the patch matches any of known values (men, women) and load the Category Page component. If it doesn't, it will load the product component and finally a 404.

Is this possible? I've considered getting the Route Path and load a component or page on this or middleware.


Solution

  • Most of the time if you want to achieve a specific routing logic overriding the natural one of Nuxt, you would use the router-module of the nuxt-community.

    Once using a classic router you would be able to create Regex to match your route since vue-router is using path-to-regex

    You would be able to achieve something like

    { path: '/:gender(\(men|women))', name: 'category', component: Category }
    

    Or any Regexp logic you want which should be enough in your case.