Skip to content

Commit 79143e2

Browse files
committed
router-links: catch errors when generating link
1 parent 741cd07 commit 79143e2

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/middlewares/router-links.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,16 @@ function updateLink (el, ownerEl) {
111111
if (!routeName) return
112112
const params = getRouteProp(ownerEl, routeName, el, 'params', 'param-')
113113
const query = getRouteProp(ownerEl, routeName, el, 'query', 'query-')
114-
const href = router.generate(routeName, params, query)
115-
const anchorEl = el.tagName === 'A' ? el : el.querySelector('a')
116-
if (anchorEl) anchorEl.setAttribute('href', href)
117-
if (!router.state.activeTransition) {
118-
updateActiveClass(el, routeName, params, query)
119-
}
114+
try {
115+
const href = router.generate(routeName, params, query)
116+
const anchorEl = el.tagName === 'A' ? el : el.querySelector('a')
117+
if (anchorEl) anchorEl.setAttribute('href', href)
118+
if (!router.state.activeTransition) {
119+
updateActiveClass(el, routeName, params, query)
120+
}
121+
} catch (error) {
122+
console.warn(`Error generating link for "${routeName}": ${error}`)
123+
}
120124
}
121125

122126
function createLinks (ownerEl, rootEl) {

tests/functional/routerLinksTest.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ class GrandChildView extends BaseGrandChildView {
9090
}
9191
const grandChildTag = defineCE(GrandChildView)
9292

93+
class BrokenView extends withRouterLinks(LitElement) {
94+
createRenderRoot () {
95+
return this
96+
}
97+
98+
render () {
99+
return html`
100+
<div routerlinks>
101+
<a id="a-brokenrootlink" route="root"></a>
102+
<a id="a-rootlink" route="root" param-id="1"></a>
103+
</div>
104+
`
105+
}
106+
}
107+
const brokenTag = defineCE(BrokenView)
108+
93109
describe('routerLinks', () => {
94110
let router, outlet, parentComponent
95111
beforeEach(() => {
@@ -103,6 +119,7 @@ describe('routerLinks', () => {
103119
})
104120
route('root', { path: 'root/:id', component: ParentView })
105121
route('secondroot', { path: 'secondroot/:personId', component: ParentView })
122+
route('brokenroot', { path: 'brokenroot', component: BrokenView })
106123
}
107124
parentComponent = ParentView
108125
router = new Router({ location: 'memory', outlet, routes })
@@ -358,6 +375,14 @@ describe('routerLinks', () => {
358375
})
359376
})
360377

378+
it('should generate href even if there is a broken link configuration', function () {
379+
return router.transitionTo('brokenroot').then(async function () {
380+
const brokenEl = document.querySelector(brokenTag)
381+
await brokenEl.updateComplete
382+
expect($('#a-rootlink').attr('href')).to.be.equal('/root/1')
383+
})
384+
})
385+
361386
describe('when elements are added dynamically', () => {
362387
it('should generate href attributes in anchor tags with route attribute', function (done) {
363388
router.transitionTo('parent').then(async function () {

0 commit comments

Comments
 (0)