Skip to content

Commit 8750504

Browse files
authored
Merge pull request #362 from sandeep-vedam/router-optional-param
Added router support for optional path
2 parents 678f072 + f324d41 commit 8750504

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

docs/plugins/router/navigation.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,46 @@ Router.navigate({
9696

9797
This results in: `#player/12/44`
9898

99+
## Optional router path
100+
101+
There might be some cases in which you need a route path with an optional parameter at the end. Normally, you would have to define two different routes to the same component, one with the optional parameter and one without. Starting with Lightning-SDK `v5.3.0`, you can specify an optional router path parameter by adding a `?` suffix to the last parameter name.
102+
103+
Please note that only the last parameter of any path is allowed to be an optional parameter. For example, if you have a route path with three parameters, you can make only the last parameter optional, but not the second parameter.
104+
105+
When we define an optional parameter like this:
106+
```js
107+
{
108+
path: 'player/:assetId/:playlistId?',
109+
component: Player
110+
name: 'player'
111+
}
112+
```
113+
114+
This will generate two paths internally as below:
115+
116+
```js
117+
{
118+
path: 'player/:assetId/:playlistId'
119+
component: Player
120+
name: 'player'
121+
}
122+
{
123+
path: 'player/:assetId',
124+
component: Player,
125+
name: 'player'
126+
}
127+
```
128+
129+
The following example would *not* work because only the last parameter of the path can be optional:
130+
131+
```js
132+
{
133+
path: 'player/:assetId?/:playlistId',
134+
component: Player
135+
name: 'player'
136+
}
137+
```
138+
99139

100140
## isNavigating Method
101141

src/Router/utils/router.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { step, navigateQueue } from '../index'
3131
import { createRoute, getOption } from './route'
3232
import { createComponent } from './components'
3333
import Log from '../../Log'
34-
import { isWildcard } from './regex'
34+
import { isWildcard, stripRegex } from './regex'
3535
import emit from './emit'
3636
import { updateWidgets } from './widgets'
3737
import { setHistory, updateHistory } from './history'
@@ -262,6 +262,28 @@ const init = config => {
262262
console.error(`[Router]: ${config.bootComponent} is not a valid boot component`)
263263
}
264264
}
265+
config.routes.forEach(item => {
266+
// replacing regexes with 'R' to avoid issues with pattern matching below
267+
const strippedPath = stripRegex(item.path)
268+
269+
// Pattern to identify the last path of the route
270+
// It should start with "/:" + any word and ends with "?"
271+
// It should be the last path of the route
272+
// valid => /player/:asset/:assetId? (:assetId is optional)
273+
// invalid => /player/:asset/:assetId?/test (:assetId? is not an optional path)
274+
// invalid => /player/:asset?/:assetId? (second path is not considered as an optional path)
275+
const pattern = /.*\/:.*?\?$/u
276+
277+
if (pattern.test(strippedPath)) {
278+
const optionalPath = item.path.substring(0, item.path.lastIndexOf('/'))
279+
const originalPath = item.path.substring(0, item.path.lastIndexOf('?'))
280+
item.path = originalPath
281+
//Create another entry with the optional path
282+
let optionalItem = { ...item }
283+
optionalItem.path = optionalPath
284+
config.routes.push(optionalItem)
285+
}
286+
})
265287
initialised = true
266288
}
267289

0 commit comments

Comments
 (0)