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 |