Routes api - Palmabit-IT/Sacajawea GitHub Wiki

ROUTES API

Routes(options)

options for constructor:

const { Routes } = require('@palmabit/sacajawea')

const routes = new Routes(options)

name required default note
locale βœ” Default language
forceLocale βœ— false Force all route to have locale on initial path
siteUrl βœ— URL of your site, used for canonical url

routes.add

This function add a new route

const { Routes } = require('@palmabit/sacajawea')

const routes = new Routes({
  locale: 'en',
  forceLocale: true,
  siteUrl: 'https://www.example.com'
})

// end of configuration

routes.add('index', 'en', '/')
name required example note
name βœ” home name of the route
locale βœ” it locale of the route. This field must always be added, even if the language of the route is the same as the default language
pattern βœ” /en/news/:slug Route pattern (see path-to-regexp) to know the right way to build perfect route
page βœ— news-list Name of file in ./pages
data βœ— { foo: 'bar' } Custom data object
update βœ— true update route with the same name and locale

routes.setLocale

This function changes the default locale

const { Routes } = require('@palmabit/sacajawea')

const routes = new Routes({
  locale: 'en',
  forceLocale: true,
  siteUrl: 'https://www.example.com'
})
// end of configuration

console.log(routes.locale) // en
routes.setLocale('fr')
console.log(routes.locale) // fr

routes.findByName

Search route by name and locale

const { Routes } = require('@palmabit/sacajawea')

const routes = new Routes({
  locale: 'en',
  forceLocale: true,
  siteUrl: 'https://www.example.com'
})
// end of configuration

routes.findByName('home','en')
name required example note
name βœ” home name of the route you want to search
locale βœ— it locale of the route searched. This field is optional, if it is not used, the default language is used

Return route object or false if route not exist

routes.findAndGetUrls

This function works similarly to "findByName", but in addition to return, the route object also returns a second object that contains the url of the searched route. However, if this route is not found, an exception is thrown

const { Routes } = require('@palmabit/sacajawea')

const routes = new Routes({
  locale: 'en',
  forceLocale: true,
  siteUrl: 'https://www.example.com'
})
// end of configuration

routes.findAndGetUrls('home','en’,{ a: 1 })
name required example note
name βœ” home name of the route you want to search
locale βœ— it locale of the route searched. This field is optional, if it is not used, the default language is used
params βœ— {} Any parameters to be passed to the route